使一个活动只出现一次,当应用程序被启动时[英] Making an activity appear only once, when the app is started

本文是小编为大家收集整理的关于使一个活动只出现一次,当应用程序被启动时的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下类, splashactivity.java :

public class SplashScreen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally{
                    Intent tutorial = new Intent(SplashScreen.this, TutorialOne.class);
                    startActivity(tutorial);
                }

            }
        };
        timer.start();
          }
}

我希望此活动只加载一次,当第一次首先在移动设备上安装应用程序时.对Android来说是新的,我对此有很少的想法.我读到了要使用的SharedPreferences的地方,但不明白实现.关于这项活动的事情是,当第一次使用时,活动必须充当Launcher,这就是真正困惑的东西.因为在清单文件中,我声明了另一个活动,在我的情况下是MainPage.java.那么我如何实现这个逻辑?我呼吁MainPage中的SplashActivity,或者是否必须完成其他东西?请帮助别人?

可以有人请写下代码,以实现这种逻辑,如果可能吗?

推荐答案

将此代码添加到您的oncreate方法

    SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    if(pref.getBoolean("activity_executed", false)){
        Intent intent = new Intent(this, TutorialOne.class);
        startActivity(intent);
        finish();
    } else {
        Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();
    }

SharedPreferences每次执行应用程序都会保留,除非您从Android上的设置清理数据. 第一次将从保存在此类首选项(ActivityPref)上保存的布尔(Activity_execute)中的值.

如果没有找到任何值,它将返回false,因此我们必须编辑首选项并将值设置为true. 下次执行将启动Activity TutorialOne.

finish()从堆栈历史中删除当前活动,因此使用来自教程中的按钮可以返回.

关于您的清单,您可以使用

设置此动态
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> 

每次执行该应用都会启动此活动,但由于"activity_executed"在"activity_executed"上设置的真实,将开始使用startActivity的新活动.

其他推荐答案

 SharedPreferences pref = getSharedPreferences("ActivityPREF",    Context.MODE_PRIVATE);
     if(pref.getBoolean("activity_executed", false)){

} else { 
   Intent intent = new Intent(this, TutorialOne.class);
    startActivity(intent);
    finish();
    Editor ed = pref.edit();
    ed.putBoolean("activity_executed", true);
    ed.commit();
} 

我认为它应该是这样的.

本文地址:https://www.itbaoku.cn/post/102579.html

问题描述

I have the following class, SplashActivity.java:

public class SplashScreen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally{
                    Intent tutorial = new Intent(SplashScreen.this, TutorialOne.class);
                    startActivity(tutorial);
                }

            }
        };
        timer.start();
          }
}

I want this activity to load only once, when the app is first installed on the mobile device for the first time. Being new to android I have very little idea about this. I read in places that the SharedPreferences is to be used, but did not understand the implementation. And the thing about this activity is that, the activity has to act as a Launcher when used for the first time, that's what really confused me. Because in the manifest file I am declaring another activity which in my case would be the MainPage.java. So how can I implement this logic ?? Do I call upon the SplashActivity in the MainPage or is there something else that must be done ?? Please help someone ?

Can someone please write down the code to implement this logic if possible?

推荐答案

Add this code to your onCreate method

    SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    if(pref.getBoolean("activity_executed", false)){
        Intent intent = new Intent(this, TutorialOne.class);
        startActivity(intent);
        finish();
    } else {
        Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();
    }

SharedPreferences will be keep every time you execute the app unless you clean the data from Settings on your Android. The first time is going to get the value from a boolean (activity_executed) saved on such preferences (ActivityPREF).

If it does not find any value it will return false, so we have to edit the preference and set the value to true. The next execution will launch the activity TutorialOne.

finish() erases the current activity from the stack history, so no come back is possible using button back from TutorialOne.

About your manifest, you can set this actitiy with

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter> 

Every time the app is executed will launch this activity, but due to the true setted on the "activity_executed" is going to start a new activity with startActivity.

其他推荐答案

 SharedPreferences pref = getSharedPreferences("ActivityPREF",    Context.MODE_PRIVATE);
     if(pref.getBoolean("activity_executed", false)){

} else { 
   Intent intent = new Intent(this, TutorialOne.class);
    startActivity(intent);
    finish();
    Editor ed = pref.edit();
    ed.putBoolean("activity_executed", true);
    ed.commit();
} 

I think it should be like this.