【问题标题】:Run Splash Layout only the First Time仅在第一次运行 Splash Layout
【发布时间】:2016-05-04 14:01:00
【问题描述】:

我只想在应用程序首次启动时启动启动画面。我的初始屏幕与另一个屏幕链接,该屏幕也是第一次出现。 我的启动画面代码是:

public class SplashPage extends Activity{
private static int startPage = 7000;
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashpage);

    prefs = getSharedPreferences("com.tech.spam", MODE_PRIVATE);

        }
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
     if (prefs.getBoolean("firstrun", true)) {
            calll();
        }
     if (prefs.getBoolean("firstrun", false)) {
         finish();
         startActivity(new Intent(SplashPage.this,MainActivity.class));
        }
            //prefs.edit().putBoolean("firstrun", false).commit();
            //finish();

}
private void calll() {
    // TODO Auto-generated method stub
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.move);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.startAnimation(anim);



    Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.move);
    TextView ivvv = (TextView) findViewById(R.id.textView1);
    ivvv.startAnimation(anim1); 



       new Handler().postDelayed(new Runnable() {          
            public void run() {    
                Intent intent = new Intent(SplashPage.this, SplashPageTutorial.class);

                startActivity(intent);

                overridePendingTransition(R.anim.fade, R.anim.hold);
                finish();
                }
            },     
        startPage);

}

现在我想在这里做的是,第一次启动应用程序时 Splash 运行 7 秒,然后打开下一个屏幕,在 7 秒后与启动链接,然后在第二个屏幕之后,我的主 Activity 开始。 现在我想做的是,当我第一次打开应用程序时,只启动主活动(启动画面和与启动画面链接的屏幕现在消失了)

我使用此代码,以便当再次打开应用程序时,布尔值返回 false 并启动 Main Activity,但它没有发生

if (prefs.getBoolean("firstrun", true)) {
            calll();
        }
     if (prefs.getBoolean("firstrun", false)) {
         finish();
         startActivity(new Intent(SplashPage.this,MainActivity.class));
        }
            //prefs.edit().putBoolean("firstrun", false).commit();
            //finish();

请帮助我该怎么办

我的清单是这样的:

<application android:icon="@drawable/appicon"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme">

    <activity android:name="com.tech.spam.SplashPage"

        android:label="@string/app_name">

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

    </activity>
    <activity
        android:name="com.tech.spam.MainActivity"

         >

    </activity>    
    <activity
        android:name="com.tech.spam.SplashPageTutorial"
        android:theme="@style/FullscreenTheme"
        >
    </activity>

【问题讨论】:

    标签: android sharedpreferences splash-screen launcher


    【解决方案1】:

    一种简单的方法是共享偏好变量。在启动初始屏幕时,将此变量设置为 true。在启动活动之前检查这个变量是否为假,只有在它为假时才启动。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        SharedPreferences settings=getSharedPreferences("prefs",0);
        boolean firstRun=settings.getBoolean("firstRun",false);
        if(firstRun==false)//if running for first time
        //Splash will load for first time
        {
            SharedPreferences.Editor editor=settings.edit();
            editor.putBoolean("firstRun",true);
            editor.commit();
            Intent i=new Intent(check.this,Splash.class);
            startActivity(i);
            finish();
        }
        else
        {
    
            Intent a=new Intent(check.this,Main.class);
            startActivity(a);  // Launch next activity
            finish();
        }
    }
    
    }
    

    【讨论】:

    • @ArslanAli 没有问题,谢谢你接受,你能不能也给答案投票:)
    • 给答案点赞,:D,现在它有 0 个点赞,按下绿色勾上方 0 上方的符号,我的答案将有一票。有助于提高声誉:D
    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多