推&QUOT区分;家"按钮,打开另一个活动按钮、QUOT

2023-09-12 05:15:04 作者:趁着新鲜

我有三个活动:   - SplashActivity   - MainActivity   - PlayerActivity

当然,应用程序开始SplashActivity,然后开始MainActivity和关闭。 MainActivity在某一时刻开始PlayerActivity并进入backstack。 (MainActivity是活的,但的onStop) 然后,我需要开放MainActivity并设置PlayerActivity背景(PlayerActivity是活的,但的onStop)。 然后,我需要再次打开PlayerActivity并设置MainActivity背景。

所以PlayerActivity和MainActivity常听到的onPause()和的onStop(),而不当的onDestroy切换应用程序彼此和背部。

我需要完成的所有活动,并启动应用程序的SplashActivity每次当用户将推动家的按钮,但home键,使活动之间的相同像开关(的onPause()和的onStop())。因此,我不能赶上差别杀活动。

请帮忙。

编辑: 不幸的是, onUserLeaveHint 的没有帮助,这是相同的。 如果用户按下HOME这就要求:

onUserInteraction, onUserLeaveHint, 在onPause, 的onStop

该活动的回报previous活动(主),没有任何用户操作。

公共类PlayerActivity延伸活动{

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_next);

    处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable(){
        @覆盖
        公共无效的run(){
            startActivity(新意图(PlayerActivity.this,MyActivity.class));
        }
    },5000);
}
 
教大家识别怎么识别假 抢红包 H5页面

}

不过还是一样的:

onUserInteraction, onUserLeaveHint, 在onPause, 的onStop

解决方案

据我所知,没有办法覆盖的home键或监听home键preSS事件。

不过,你的目标是让应用程序知道并采取行动时,会发生以下情况:

您没有任何活动都显示 - >你的一个活动,是展示

当发生这种情况时,要显示启动对话框。

您可以继续当用户在应用程序的跟踪和检查用户是否可以在程序中导航到你的活动。

更新:而不是修改活动如下面的例子显示,你可以使用 ActivityLifecycleCallbacks 对象知道,当你的任何活动生命周期回调被调用。你可以把我的例子并对其进行修改。我相信ActivityLifecycleCallbacks.onActivityStarted()被调用的super.onStart()调用之后,所以你必须检查cameFromMyApplication()调用super.onStart()的Activity.onStart()之前。这是不容易出错,并且需要更少code

  

修改How检查活动是在前台或后台可见?,以适应这个问题

     

示例   实现自定义的应用程序类:

 公共类MyApplication的扩展应用{

  公共静态布尔cameFromMyApplication(){
    返回计数!= 0;
  }

  公共静态无效activityStarted(){
    算上++;
  }

  公共静态无效activityStopped(){
    计数 - ;
  }

  私有静态诠释计数;
}
 

  

填写您的应用程序类在AndroidManifest.xml中:

 <应用
    机器人:名称=your.app.package.MyApplication
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME>
 

  

添加ONSTART和的onStop每一个活动项目(可能   创建一个共同祖先的活动,如果你想):

  @覆盖
保护无效的OnStart(){
  super.onStart();
  //不包括此项检查在启动画面活动
  如果(!MyApplication.cameFromMyApplication()){
    //用户从应用程序之外抵达
    //具体应用code(清空行业backstack和放大器;展现在你的情况闪屏)
  }
  MyApplication.activityStarted();
}

@覆盖
保护无效的onStop(){
  super.onStop();
  MyApplication.activityStopped();
}
 

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:

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();
}

 
精彩推荐
图片推荐