Wednesday, April 13, 2011

Go back to previous activity

Its every common query how to go back to previous activity.
Most of us will finish activity or keep track of the activity stack and all.
But no need of this type of complex logic if you need just the same effect of user's back key press
You can use the android onBackPressed() method explicitly .

super.onBackPressed();

The default implementation simply finishes the current activity, but you can override this to do whatever you want.
The android implicitly call this method when the activity has detected the user's press of the back key.
So calling this explicitly will give the same effect 

9 comments:

  1. Hello, actually using that method closes my first activity (in wich i execute that super.onBackPressed();) and not the second activity in wich i dont have access.

    In my case, my app work making a call and then i want to bring my onPaused activity to the front again... I mean like not closing the Call, but to show my activity again..

    Thanks for your help

    ReplyDelete
  2. Hi fr0st, The default implementation simply finishes the current activity.
    I didn't get you completely. if you want to call an previous activity without finishing current activity, best thing is start that activity again and you should set the launchMode as singleTask

    ReplyDelete
  3. Sorry english isnt my mother language, but what im trying to say is that:

    I got myActivity wich in certain part of the code makes a call using:

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+nume));
    startActivity(callIntent);

    wich then opens up the default dialer and start calling tjat nume number.. i just need that my same myActivity return back to the screen, life if the user in the calling activity pressed back button

    So i need to make some code in onPause() that will emulate the user pressing back button but in the calling app of the android (wich of course i dont have access to the code)

    Hope you understand me and im really in a hurry with this so an ASAP reply would be appreciated

    ReplyDelete
  4. Like i just need that onBackPressed(); works on the whole system, not only locally at my activity

    ReplyDelete
  5. I think in your case it is better to use startActivityForResult, so it will return to onActivityResult

    ReplyDelete
  6. Am new to Android, I feel it will be better if u explained the above topic with some Example code.

    ReplyDelete
  7. how to link the R.ID.textedit(nuber) in java variable creation of name

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. as I am triggering google map app for navigation..it will take me to navigation screen but while i click on back button it will show two more activity after that it will return back to original user app..can we go into main app in single click??

    ReplyDelete