sadegh saati
sadegh saati

Reputation: 1170

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

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?

Upvotes: 1

Views: 1232

Answers (1)

Can Gokdere
Can Gokdere

Reputation: 286

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);
}

Upvotes: 1

Related Questions