Issue
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
Solution
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:
- None of your Activities are showing -> One of your Activities is showing.
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.
Modified How to check if activity is in foreground or in visible background? to fit this question
Example Implement custom Application class:
public class MyApplication extends Application {
public static boolean cameFromMyApplication() {
return count != 0;
}
public static void activityStarted() {
count++;
}
public static void activityStopped() {
count--;
}
private static int count;
}
Register your application class in AndroidManifest.xml:
<application
android:name="your.app.package.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >
Add onStart and onStop to every Activity in the project (you may create a common ancestor for your Activities if you'd like to):
@Override
protected void onStart() {
super.onStart();
//Do not include this check in the splash screen Activity
if(!MyApplication.cameFromMyApplication()) {
//User arrived from outside the application
//Application specific code (clear Activity backstack & show splash screen in your case)
}
MyApplication.activityStarted();
}
@Override
protected void onStop() {
super.onStop();
MyApplication.activityStopped();
}
Answered By - vman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.