0

I have following situations with activities A,B,C: A->B->C->A

In this last step (C->A), I want to override onBackPressed of C, so that it restarts activity A (without recreating it). I tried code below, but onCreate() of A is still called. Which flag should I add?

public void onBackPressed() {

    Intent intent=new Intent(C.this, A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

}
Zvone Peran
  • 53
  • 1
  • 11

5 Answers5

1

This is the best way to refresh your activity:

public void refresh() {
   Intent intent = getIntent();
   overridePendingTransition(0, 0); 
   intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
   finish(); 
   overridePendingTransition(0, 0); 
   startActivity(intent); 
   }
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
0

OnCreate will be called, this is the correct behavior, below is from Google documentation:

When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

but this does not matter if you handle it properly, for instance:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}
Community
  • 1
  • 1
David
  • 15,894
  • 22
  • 55
  • 66
0
public void onBackPressed() {

    Intent intent=new Intent(C.this, A.class);
    // remove below Flag and while going from A dont call finish();
    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

}
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

So you want to make sure, that activity A is in the same state as it was before you switched to activity B, right?

Have you tried using the onSaveInstanceState() / onRestoreInstanceState() (respectively onCreate(Bundle savedInstanceState)) callbacks as described in: https://developer.android.com/training/basics/activity-lifecycle/recreating.html ? Example here: https://stackoverflow.com/a/151940/3540885

I am not sure if your desired way is possible. Usually it is better to let Android handle the lifecycle itself. Just save the important stuff whenever the system feels like destroying your activity - so you can recreate it with the last state again...

edit: It is long ago since I last messed around with multiple activities. Have you considered using Fragments instead of multiple activities? Once I got the concept of the Fragments, I started using only one parent Activity and replacing Fragments. So all your "heavy stuff" could be instantiated once in the parent activity and accessed by the fragments who need it. This could be an alternate solution, which is IMHO worth to think about.

Community
  • 1
  • 1
AZOM
  • 265
  • 5
  • 15
  • Yes, I want to make sure that A is in the same state as it was before switching to B, but there is one more reason. onCreate() of activity A is doing some heavy stuff which should be executed only once, so I would like to avoid repeating it because of power consumption. – Zvone Peran Jul 15 '16 at 12:31
  • Still I think you should work with the Bundle "savedInstanceState". Is the **"heavy stuff"** GUI related? Could you extract the logic and do it somewhere else (maybe Application class)? – AZOM Jul 15 '16 at 12:41
  • Yes, it is GUI related. I will explore your suggestion. – Zvone Peran Jul 15 '16 at 13:09
-1

I like Stanojkovic answer and to save data you can pass it using the intent.

 public void refresh() {
   Intent intent = getIntent();
   intent.putExtra("extra_id",details);   
   overridePendingTransition(0, 0);
   intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
   finish(); 
   overridePendingTransition(0, 0); 
   startActivity(intent); 
   }

and to get it in onCreate

details=getIntent().getStringExtra("extra_id")
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17