how to implement quit functionality in android application - android

I want to use quit functionality in my android application. Is there any way to kill all running activities on back button press.Actually i am showing a dialog(contains two buttons) on a button click and by clicking the one of the button on dialog box invokes another activity.But the previous activity by which the dialog is shown, is not finished. Is there any way to finish the activity when i click one of the button of the dialog.
How can i use finish method to end the previous activity.This extends dialog.
#Override
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("co.y.c", "co.y.c.activityToStart");
v.getContext().startActivity(i);
dismiss();
});

You can call Activity.finish().

you can close your app/activity by calling this.finish().
Cleanup should be done within onPause(...) / onFreeze(...) / onDestroy(...) depending on what you want to do.
Regards,
Arun Kumar

If you don't want the previous Activity to be shown on the back button press, you can specify this in the AndroidManifest.xml like:
<activity android:name="sample.activity.MyActivity" android:noHistory="true" />
This will prevent the Activity from being put into the history stack and allow the back button to quit (assuming you add the noHistory flag to every previous Activity)

Related

back button's function on first activity

Normally when the back button is clicked it goes to the previous activity and if the current activity is the first activity, the application closes.
I have a splash screen (that is logically my first activity) and then the menu activity loads.
I want to close the program when the back button is pressed on my menu activity (as if it is the first activity) and avoid going back to the splash screen again, but I know that I should not exit the program.
I was wondering what is the functionality of back button on the first activity?
does it put the program to pause?
Avoid splash screens !
Instead of using your splash as the main activity, use menu activity as the main one, and in onCreate() chain off the splash activity, which will close and disappear forever.
When new Activity is launched from first Activity, first Activity is executed until onStop() method, then it stops and waits for relaunch, unless you killed it by calling .finish(), in that case launched Activity becomes first Activity and back button will minimize application on back button press. In order to control what application does on back button press you can override this method in your Activities and implement your own custom behaviour:
#Override
public void onBackPressed() {
super.onBackPressed()
}
Very good example of how lifecycle of Fragments/Activities work can be seen in picture below:
Hope this helps. Good luck.
While you are on the SplashScreen, on the method calling your menu activity I assume you are doing something like startActivity (new Intent (this, MenuActivity.class)); in Java or startActivity(Intent(this, MenuActivity::class.java)) for Kotlin just after that call finish() this will remove your SplashScreen from the back stack
add this attribute to your splash activity (in manifest):
android:excludeFromRecents="true"
pressing back in per activity, transfer control to prev activity in stack (back stack) that not excluded from "Recents".

How to clear activity stack trace on back press event?

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!

Disable an android button pressing twice

I have an activity which contains a back button. when it is pressed, it starts another activity which contains a back button in same position. But the problem is it takes few seconds to start activity because it does some heavy task in background. Now the problem is, if the user press back button again before starting the new activity, the second press goes to new activity and the it navigates from new activity to another new activity. I think the problem is clear to you. Any idea how to solve this problem??? Thanx.
Make the Back Button invisible until the new activity is loaded.
You can disable the button when pressed once like
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button1.setEnabled(false);
}
});
It sounds like you are blocking the main thread to perform your "heavy task", I would advise that you start the new Activity, then onCreate fire an AsyncTask to perform your "heavy task".
This way the view will load fast and you can show a dialog to the user to say that it's loading.
Link to AsyncTask tutorial:
http://droidapp.co.uk/2011/05/12/android-dev-pre-loading-with-asynctask/

block back button for ceratin activities

I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}

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