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();
}
}
);
Related
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();
}
I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.
As of yet I have been using something similar to the function below :
#Override
public void onBackPressed()
{
this.finish();
Intent int1= new Intent(this, Home.class);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(int1);
super.onBackPressed();
}
But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)
I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code.
Any help on the above is appreciated!
EDIT :
Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.
You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):
android:noHistory="true"
This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.
Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.
Anytime during the flow of my activity if the user presses the back
button, I want the control to be thrown back to the main page
(clearing all the previous activity stack traces).
This can be done just by finishing all the activities as they move forward, except the MainActivity.
Such that in case someone re-lanches the application it will re start
normally from the main page.
Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?
IF so, you can call finish() in onPause() of every Activity except MainActivity
EDIT:
Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):
OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.
#Override
protected void onResume() {
super.onResume();
SharedPreferences mSP = getSharedPreferences(
"your_preferences", 0);
if (mSP .getBoolean("backPressed", false)) {
finish();
}
}
Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.
http://developer.android.com/guide/topics/ui/actionbar.html
Also check this link and comments below the answer by warrenfaith
android - onBackPressed() not working for me
#Override
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
you can use that code, it's work for me!
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.
How can I kill all the Activities of my application?
I tried using this.finish() but it just kills one Activity.
In fact, I would like when the user touches the back button (in only one of the activities), the application to do the same as if he pressed the Home button.
You can set android:noHistory="true" for all your activities at AndroidManifest.xml. Hence they will not place themselves onto history stack and back button will bring you to the home screen.
This has been discussed many times before. Essentially there's no easy way, and there's not supposed to be. Users press Home to quit your app (as you have pointed out).
Override the onBackPressed method, and do what happens when they press the home button. It's not really closing all the activities but what you're asking is that the back press in a certain activity emulates the home button.
#Override
public void onBackPressed() {
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
Placing that in an activity will go home when you press back. Note that the original implementation of the onBackPressed method includes a call to super, it's removed on purpose. I know this question is super old, but he's explicitly asking something else
To kill all the activities, use these code on onBackPressed.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount()>0) {
getFragmentManager().popBackStackImmediate();
} else {
finishAffinity();
System.exit(0);
}
}
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();
}
});