user3249859
user3249859

Reputation: 21

how to return back to previous activity on back pressed?

i have call Activity A with state 2 from Activity A with state 1 then again call Activity A with state 3 from Activity A with state 2. Now how can i go back to Activity A with state 2 when i press on back button in android?

Upvotes: 1

Views: 11287

Answers (3)

user8442578
user8442578

Reputation:

@Override 
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
    Intent intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(i);
    finish();

}

You must Declare i to Intent, It's work for me ( API 23+ )

Upvotes: 0

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

You can do this using following code-

@Override 
public void onBackPressed() {
super.onBackPressed();
i = new Intent(CurrentActivity.this, NextActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i);
finish();
} 

Upvotes: 3

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can override onBackPressed for that.

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    ActivityB.this.finish();
}

Upvotes: 1

Related Questions