Close Application and Launch Home Screen on Android

Close application and launch home screen on Android

You should really think about not exiting the application. This is not how Android apps usually work.

How to close an application when the Home button is pressed

There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.

How to exit from the application and show the home screen?

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

How to program an application on launch to close and automatically open the home screen (Launcher) (Wear OS)

Your intent is correct. You are just missing the last bit to actually initiate the intent.

val startMain = Intent(Intent.ACTION_MAIN)
startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startMain.addCategory(Intent.CATEGORY_HOME)
startActivity(startMain) // <- Start the intent

Android: Closing the application and opening it from recent list

i will put my thoughts...

  1. why r u checking sharedpreference in B at all? if u have stored in sharedprefernce redirect the user directly to C from A .

    if(sharedPreference) -> C

    else B -> C

  2. when u are launching from recent afaik it will call onresume and not oncreate and also i believe you are checking in oncreate hence its not executing when launching from recent , use logs to know the cycle also please refer to activity life cycle.

  3. if u dont need the activity to exist u can call finish() and also put noHistory='true' in manifest...

plz see to these points and rectify me if i m wrong anywhere...

thx and hope it helps u...

How to close entire application?

I Know it is possible.

Not at your will though.

android.os.Process.killProcess(android.os.Process.myPid());

A note from the documentation about this method:

Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

About system.exit(0) , read this answer. And keep that in your mind.

And about finish(), the documentation clearly says the following:

"Call this when your activity is done and should be closed."

To sum it all up - don't try to make a task killer. The android OS takes care of that perfectly well. Don't meddle with it.

The wheel has been invented and is being used perfectly, don't reinvent it.

Please read this. You need to understand that Android doesn't require task killers. Google it and you will find many such articles.

how to finish all activities and close the application in android?

There is finishAffinity() method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.



Related Topics



Leave a reply



Submit