problem with going back to last activity via custom back button - android

I have a custom back button that for the time being does nothing other than going to the previous activity on the back stack. Heres the code for the button :
backButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! BACK !!!");
finishActivity(0);
}
});
The problem is, its not working. There is simply no change.
Can anyone kindly tell me what I have done wrong here ? Thanks.

finishActivity finishes an activity that you had previously started with startActivityForResult(), it doesn't finish the activity you are currently in - finish() does that.

Use finish() instead of finishActivity().
finishActivity() forces an activity that you started via startActivityForResult() to quit, it doesn't finish the current activity. You can also use onBackPressed(), that just calls finish() internally (see the default implementation), so it has the same effect. Useful if you override the back button behaviour though.

Related

How can I preserve previous android intent value when I press the back or up button?

I have a list view in the previous intent and when I tap on that it go to the next activity. Now when I press the back button or up button it comes back to the previous activity. But when I tap the list item again it give me run time Unexpectedly app closed error. Please help me.
It's hard to answer without code, but maybe it's because you don't finish your next activity when you press the back button
It's because of Activity life cycle...
When you go to the next activity then current activity is paused for you. when you press the back or up button then your previous activity resume.
That's why you have to define your code in onResume() method also where you got stuck. because when you come back onResume method is called
You can try override onBackPressed method in your activities :
#Override
public void onBackPressed() {
// put a breakpoint here and check your listview/activities states
//super.onBackPressed();
}

Restart Activity with back button

Basically my MainActivity has a button which will become invisible after clicking and the SecondActivity will be called after a few seconds. However, when I press the back botton, the button on the MainActivity is still invisible. I want the MainActivity to restart/initialize. I knew it is something to do with onResume or onRestart but how can I implement these methods? Can anyone give me an example? Thanks.
You could call finish() on your MainActivity when you go to your second one. Then Override onBackPressed() in your SecondActivity and start the MainActivity again.
#Override
public void onBackPressed()
{
// create Intent and start MainActivity again
}
I think you are looking for startActivityForResult. You can see a sample of usage in the Android documentation or here on SO.
Basically in your first activity you overwrite the method onActivityResult and in it (if the result is OK) re-show the button. Then, in the second activity, set the result to be returned to be OK and just finish it however you like (either by pressing the back button or by calling finish()).
Alternatively:
You may ovewrite the onResume method in the first activity and just show the button every time this method is called (note that onResume is called even on activity's first start, but since the button is already shown in your case - it won't have any effect).
#Override
public void onResume(){
Button b = (Button) findViewById(R.id.myButton);
b.setVisibility(View.VISIBLE);
}
Inside your Activity, simply write
#Override
public void onResume(){
// put your code here...
yourButtonInstance.setVisibility(View.VISIBLE)
}
and put the logic you need to change the visibility inside it
You could set your button as attribute of the activity and make your button visible in the onPause() or in the onResume() method.
button.setVisibility(View.VISIBLE);

How to close activities which are appeared only once?

I am creating an app which consists of main, register, option activities. Main and register activities should be started only once. I got it. Problem is they are again appearing when I just going back to my mobile home page without clicking exit. Even if I do not click the exit and goes back to home, they should not appear again when I entered!! Is this possible? I am not able to get the solution for this. Any one please help me.. Thankyou
you can override the method onStop() and put finish() there.
So, when the activity will be not longer visible, it will finish. Or you can put finish() after startActivity() call method.
you can do this by.
Button button15 =(Button)findViewById(R.id.button5);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
}
);

android Back button functionalit

I just need to know this. Since calling the finish() in the activity takes you to the previous activity, when you press the Back button it actually finishes the current activity and takes you to the previous activity?
When the user presses the back button on the current activity it is popped from the activity stack and destroyed and the previous activity is resumed with its re-stored state.
Read Tasks and Backstack for a more detailed information. It is essential that you understand this concept thoroughly.
Hope this helps!!
This would be true if you haven't gotten rid of the activity by say calling finish() on your previous activity. You also can change the functionality by calling
#Override
public void onBackPressed() {
// do something on back.
return;
}

How do I return to a calling activity if the user presses BACK?

My MAIN activity is spawning a child activity that contains a ListView. While this ListView is being populated (through an AsyncTask), an indeterminate progress bar is shown.
However, assuming that I am an impatient user and I press the BACK button, the progress bar is cancelled but I am left with a blank screen. I have to press BACK one more time to go back to the MAIN activity.
I would like the app to go back directly to the MAIN activity by pressing BACK only once. Can somebody point me in the right direction? I am thinking I should call finish() somewhere but I don't know where to put it. Thanks in advance!
Use setOnCancelListener() on Dialog (which ProgressDialog extends).
you should override "onBackPressed()"
it will call when the activity has detected the user's press of the back key.
#override
public void onBackPressed(){
this.startActivity(this,MainActivity.class);
}
this code will call the MainActivity when emulator/ipphone back button pressed...
you can try like this,place this code in your activities oncreate block.
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});

Categories

Resources