Vishesh Chandra
Vishesh Chandra

Reputation: 7071

Activity lifecycle issues

I have 3 activities running ,navigating a1 to a2 then a2 to a3.Now pressing back button from emulator,I want to go to the activity a1 without finishing activity a2.How can i do this..Please guide me..Thanks in advance

Upvotes: 1

Views: 637

Answers (6)

Naresh Sharma
Naresh Sharma

Reputation: 4323

Yes you can override that back button

public void onBackPressed() {

 Intent start = new Intent(currentclassname.this,which activity u want.class);
    startActivity(start);
    finishActivity(0);
 }

By this you can move on any activity. This is very easy and simple way

Upvotes: 7

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

In a2 activity

you can overide the restart() as below::

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
startActivity(new Intent(getApplicationContext(),a1.class));
}

Upvotes: 0

Abhishek Modi
Abhishek Modi

Reputation: 41

You can also implement this from your AndroidManifest.xml file, just adding android:noHistory="true" attribute in those you want.

Upvotes: 0

Bhavin
Bhavin

Reputation: 6010

Write this thing in a3

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent intent=new Intent(getApplicationContext(),a1.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

}

Upvotes: 1

jjNford
jjNford

Reputation: 5270

Activity 2 does not have to die. Just implement the onPause() and onResume() methods o hold and restore the activity state.

http://developer.android.com/reference/android/app/Activity.html

The life cycle diagram is helpful. To control he way activities are generated and moved on he stack you can adjust the manifest activity properties or add flags to the calling intent.

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128448

Try this in your A3 activity's back key event:

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

Upvotes: 3

Related Questions