Navigate to activity by back button doesn't call onstart()

1.2k views Asked by At

I have an activity(A) and I need to set some text after activity becomes visible to user ,first time I navigate to activity everything is OK,but when I navigate from (A) to activity (B) and press back button ,it returns to (A) button doesn't call onstart of (A).what is the problem?

1

There are 1 answers

2
Can Gokdere On

Back button is navigation to previous activity in activity stack, which is already created and thus its onResume method will be called. So you can do what you want inside onResume().

If it is must for your activity to create new instance do as follows: If you are on activity A and going B, call A.finish() so it discards A from activity stack, and on B override backPressed and create a new instance of A.

@Override
public void onBackPressed() {
   Intent i= new Intent(this, A.class);
   i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   this.finish();
   startActivity(i);
}