5

I have two activities A, B . Now from A i call B by pressing a button (using startActivity()) , then press Back key to go back to A . Now when i press Button again to go to B , fresh activity is called (as expected).

Now can someone tell me how to show old previous state of B ?

I have read this article Saving Android Activity state using Save Instance State , but couldn't help myself :(

public class B extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    if(savedInstanceState!=null){
        EditText editText=(EditText)findViewById(R.id.editText1);
        editText.setText(savedInstanceState.getString("EditBox"));
    }
}

@Override
protected void onSaveInstanceState(Bundle onSaveInstanceState) {
    System.out.println("B.onSaveInstanceState()");
    super.onSaveInstanceState(onSaveInstanceState);
    onSaveInstanceState.putString("EditBox","Hello");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    System.out.println("B.onRestoreInstanceState()");
    super.onRestoreInstanceState(savedInstanceState);
    EditText editText=(EditText)findViewById(R.id.editText1);
    editText.setText(savedInstanceState.getString("EditBox"));
}}

My Class A

public class A extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button=(Button)findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent i=new Intent(StartActivityforresultActivity.this,B.class);
             startActivity(i);
        }
    });


}
Community
  • 1
  • 1
Code_Life
  • 5,742
  • 4
  • 29
  • 49

1 Answers1

6

With what it sounds like you're trying to do you have two options: 1. Save the state of B when B's onDestroy or onBackPressed is called. You'll have to save this to memory or write it out using some sort of persistence (SharedPreferences, local file, etc). Then whenever B is started, check to see if that data exists and use it to load the state. 2. Override onBackPressed so that when it is pressed you aren't calling super.onBackPressed. Instead start an instance of activity A and set your intent's flags to be FLAG_ACTIVITY_REORDER_TO_FRONT before calling startActivity. So something like this:

    Intent intent = new Intent(this, A.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);

Now when you hit back, it should find the instance of A that is in your activity stack and just bring it to the front. You may have to add the same flag whenever you start B as well.

Chris
  • 3,007
  • 2
  • 19
  • 21
  • Thanks , Your solution is working perfectly (i have tried option 2). But i dont want to override onBackPressed , as i dnt knw which activity has called the current activity to go back i use activity stack . To be more clear i hve A,B,C,D Activity , now A calls B , C (depending on some condition) which in turn calls D . I hope U get it :) – Code_Life Dec 01 '11 at 05:10
  • Can you give me some more information on the flow of events. Activity A goes to B and if you hit back it should go to A. Activity B goes to C and if you hit back it should go to B, then A. etc? – Chris Dec 01 '11 at 06:25
  • Screen flow A>B>D(A go to B and B goes D) , A>C>D . Now when i am at D i dnt knw which activity has called this activity . So i was looking for some generic solution. (Although i could use some parameter in intent, but i am looking for some generic solution ) – Code_Life Dec 01 '11 at 06:59
  • It looks like what you want to accomplish is use the default back button functionality but not have it call finish() on the activity when you hit back (which is done by default). I'm not sure if this is possible. In this situation I think you may be stuck saving something somewhere. One possibility you could look at is overriding the Application object and storing an ArrayList of class names in. You could add the class name every time you advance an activity and then pop one off and remove it whenever you hit back. – Chris Dec 01 '11 at 14:24
  • 1
    @Chris Thanks chris, u solved my problem, +1 for that. Don't know why this question and this answer hasn't got enough votes. Only one that solved my problem :) – mfs Jul 11 '14 at 03:32