I have three activity: - SplashActivity - MainActivity - PlayerActivity
Of course the app starts with SplashActivity, then it starts MainActivity and closes. MainActivity in some moment starts PlayerActivity and goes to backstack. (MainActivity is alive but is onStop) Then I need open MainActivity and set PlayerActivity to background (PlayerActivity is alive but is onStop). Then I need open PlayerActivity again and set MainActivity to background.
So PlayerActivity and MainActivity often gets onPause() and onStop() without onDestroy when app switch one to another and back.
I need finish all activities and start app for SplashActivity each time when user will push "home" button but home button makes the same like switch between activities (onPause() and onStop()). So I can not catch the difference to kill activities.
Please help.
EDITED: Unfortunately, onUserLeaveHint doesn't help, it's the same. If User pushes HOME this calls:
onUserInteraction, onUserLeaveHint, onPause, onStop
This Activity return previous Activity (Main) without any users actions.
public class PlayerActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(PlayerActivity.this, MyActivity.class));
}
}, 5000);
}
}
But still have the same:
onUserInteraction, onUserLeaveHint, onPause, onStop
To my knowledge, there is no way to override the home button or listen for home button press events.
However, your goal is to have the application know and take action when the following occurs:
When this occurs, you want to show a splash dialog.
You can keep track of when the user is in your application and check whether the user navigated to your Activity from within your application.
UPDATE: Instead of modifying all Activities as the example shows below, you could use the ActivityLifecycleCallbacks object to know when any of your Activities' lifecycle callbacks are called. You can take my example and modify it. I believe ActivityLifecycleCallbacks.onActivityStarted() is called after the super.onStart() call, so you will have to check cameFromMyApplication() before you call super.onStart() in Activity.onStart(). This is less prone to error and requires less code.
you can check it like this:
You can catch a "home button click" with the overridable
onUserLeavesHint()
function, that may be sufficient in your situation.Documentation from http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
I was looking for a solution to a similar problem and the code provided by ungalcrys above put me in the right direction so here's a more universal solution to the problem and for anyone that needs it