I have 2 Activity A and B, in Activity A I have 1 EditText and 1 Button. The Button is use for calling Activity B. I just wondering, when the user input some text in EditText in first time, then user calling Activity B, After that Back to Activity A (A > B > Then Back to A) can I still have user input Any Help is needed Thank you :D

Here my code for Activity A (CreateData is Activity A)

public class CreateData extends Activity implements OnClickListener{
    private Button buttonAdd;
    private EditText edNama_sma;
    private String nama;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_data);
        edNama_sma     = (EditText) findViewById(R.id.nama_sma);
        buttonAdd      = (Button) findViewById(R.id.buttom_add_koordinat);
        buttonAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        nama  = edNama_sma.getText().toString().toUpperCase().trim();
        // TODO Auto-generated method stub
        switch(v.getId())
        {
            case R.id.buttom_add_koordinat:
            Intent c = new Intent(this, ActivityB.class);
            startActivity(c);
            finish();
        }
    }
@Override
public void onResume() {
    super.onResume();

        edNama_sma.setText(edNama_sma.getText());

    }
}
有帮助吗?

解决方案 2

Override the onResume() method and inside this method set the value of name as text of EditText named edNama_sma.

@Override
public void onResume() {
super.onResume();

    edNama_sma.setText(nama);

}

So, whole activity class should be look like

public class CreateData extends Activity implements OnClickListener{
    private Button buttonAdd;
    private EditText edNama_sma;
    private String nama;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_data);
        edNama_sma     = (EditText) findViewById(R.id.nama_sma);
        buttonAdd      = (Button) findViewById(R.id.buttom_add_koordinat);
        buttonAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        nama  = edNama_sma.getText().toString().toUpperCase().trim();
        // TODO Auto-generated method stub
        switch(v.getId())
        {
            case R.id.buttom_add_koordinat:
            Intent c = new Intent(this, ActivityB.class);
            startActivity(c);

        }
    }

    @Override
    public void onResume() {
        super.onResume();

        edNama_sma.setText(nama);

    }
}

其他提示

Don't call finish when you start the other activity. Then the original activity will stay in the activity stack and the data in the edit text will still be populated. Otherwise you should use SharedPreferences to store any temporary data you need to retain when you leave an activity. Sometimes if the device rotates it can wipe the contents of input views, in which case you need to save and reload them using SharedPreferences.

http://developer.android.com/reference/android/content/SharedPreferences.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top