Distinguish between pressing the home button and opening another action

I have three actions: - SplashActivity - Main activity - PlayerActivity

Of course, the application starts with SplashActivity, then MainActivity starts and closes. MainActivity at some point starts PlayerActivity and goes back and forth. (MainActivity is alive, but onStop is located) Then I need to open MainActivity and set PlayerActivity for the background (PlayerActivity is alive, but it's on). Then I need to open PlayerActivity again and set MainActivity for the background.

Thus, PlayerActivity and MainActivity often get onPause () and onStop () without onDestroy when the application switches from one to the other and back.

I need to complete all the actions and launch the SplashActivity application every time the user presses the "home" button, but the home button does the same as the switch between the actions (onPause () and onStop ()). Therefore, I can’t understand how to kill actions.

Please, help.

Editorial: Unfortunately, onUserLeaveHint does not help, the same thing. If the user presses HOME, this calls:

onUserInteraction, onUserLeaveHint, OnPause, OnStop

This operation returns the previous activity (Home) without any user action.

the public class PlayerActivity extends the action {

@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 there is the same thing:

onUserInteraction, onUserLeaveHint, OnPause, OnStop

+5
source share
4 answers

As far as I know, there is no way to override the home button or listen to home button press events.

However, your goal is to know the application and take action when the following happens:

  • None of your operations are shown β†’ One of your actions is shown.

When this happens, you want to show a splash dialog.

You can track when a user is in your application and check if the user has been moved to your activity from your application.

UPDATE: instead of changing all the actions, as shown in the example below, you can use the ActivityLifecycleCallbacks object to find out when any of your lifecycle callbacks are called. You can take my example and modify it. I believe that ActivityLifecycleCallbacks.onActivityStarted () is called after calling super.onStart (), so before calling super.onStart () in Activity.onStart () you will need to check comeFromMyApplication (). This is less error prone and requires less code.

Start of change How to check if activity is working in the foreground or in the visible background? to fit this issue

Example Implement your own 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 each activity in the project (you can create a common ancestor for your activity if you want):

 @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(); } 
+2
source

You can catch the "home button click" button with the overridable onUserLeavesHint() function, which may be sufficient in your situation.

 @Override public void onUserLeaveHint() { super.onUserLeaveHint(); // Do your thing } 

Documentation from http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint ()

protected void onUserLeaveHint ()

Called as part of the life cycle of an activity when an activity exits in the background as a result of a user’s choice. For example, when the user presses the Home key, onUserLeaveHint () is called, but when an incoming phone call calls activity, the call is automatically brought to the forefront, onUserLeaveHint () will not cause an interruption in activity. In cases where it is called, this method is called right before the onPause () callback activity.

This callback and onUserInteraction () are intended to aid in the action of notifying status bar messages intelligently; in particular, for helping events determine the right time to cancel the notification.

+1
source

you can check it as follows:

 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); // check if home was pressed if (tasks.get(0).topActivity.getPackageName().equals("com.android.launcher")) { // HOME button pressed } else { // RECENT APPS pressed (you can listen for the key event before) // activity with FLAG_ACTIVITY_NEW_TASK started // etc. } 
+1
source

I was looking for a solution to a similar problem, and the code provided by ungalcrys above set me in the right direction so that there is a more universal solution to the problem for those who need it.

 @Override protected void onStop() { super.onStop(); ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); // check if the app is still visible if (!tasks.get(0).topActivity.getPackageName().equals(getPackageName())) { // for some reason(HOME, BACK, RECENT APPS, etc.) the app is no longer visible // do your thing here } else { // app is still visible, switched to other activity } } 
0
source

Source: https://habr.com/ru/post/1205739/


All Articles