繁体   English   中英

在后台杀死应用程序后,每次用户进入应用程序时显示布局

[英]show layout every time the user enter the application after app has been killed from background

我想在应用程序启动期间使特定布局可见,而不是在每次用户访问应用程序中的活动时活动启动时,有人可以告诉我该怎么做吗?

我已经尝试过 onstart() 方法并阅读了很多答案,但大多数答案都是关于背景和前景的,它们不是我想要的,onstart() 每次访问活动时都会显示布局。

我只希望布局在用户从手机访问应用程序时显示一次(例如,当应用程序在后台被杀死或之前/在当天第一次打开之前不在后台时)。 一次性的事情,但不是在第一次使用应用程序时而是每次用户进入应用程序时(在应用程序不在/从后台删除之后)。

请帮忙,提前谢谢。

使用 SharedPrefences,如下所示:

public class MainActivity extends Activity {


    SharedPreferences sp;
    Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     sp = getSharedPreferences("mypreference",
            Context.MODE_PRIVATE);
    editor = sp.edit();

  }

   @Override
    protected void onStart() {
    super.onStart();

    //we set default value to true because if it's not initialized yet, it is first time!
    if(sp.getBolean("key_first_time" , true)){

     //it's app first time start up
     ....
     //change status of shared preference to false
     editor.putBoolean("key_first_time" , false);
     editor.apply();
    }else{

     //it's not app first time start up

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();

  //this method called when your activity is not in memory anymore

 //change status of shared preference to true
 editor.putBoolean("key_first_time" , true);
 editor.apply();
}

}

请注意,当从 android 内存(后台)中删除活动时,我们的 sharedprefrence 将更改为 true,换句话说,我们的第一次状态将重置。

我认为简单的解决方案只是在相同的布局中创建布局(您与活动一起使用)(两个布局都应该覆盖全屏,您可以通过相对布局实现这一点)并在 XML visibility="Gone" 中设置您想要的布局显示几秒钟,现在当活动开始时设置 id.setVisibility=VIEW.VISIBLE 布局你想显示几秒钟并设置倒计时 3/4 秒和倒计时完成设置 id.setVisibility=VIEW.GONE ,, 为此您必须使用相对布局,另一个解决方案是如果您的活动是启动器活动,则可以使用启动画面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM