【问题标题】:Dangerous mix Splash + Shared Preferences危险混合飞溅 + 共享偏好
【发布时间】:2015-11-08 12:55:44
【问题描述】:

我有一个 Splash 活动,我希望通过 Intent 将用户重定向到不同的类,具体取决于它是否是第一次访问应用程序。

除了速度很慢之外,它还会崩溃

public class MainActivity extends Activity {

    private long splashDelay = 1500;
    int counter;
    SharedPreferences app_preferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        counter = app_preferences.getInt("counter", 1);
        System.out.println("count is..." +counter);

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                finish();
                if(counter==1){
                    Intent in = new Intent(MainActivity.this, Attempt.class);
                    startActivity(in);
                }
                else{
                    Intent intent = new Intent().setClass(MainActivity.this, MainPage.class);
                    startActivity(intent);
                }
                SharedPreferences.Editor editor = app_preferences.edit();
                editor.putInt("counter", +(counter+1));
                editor.commit();
            }
        };

        Timer timer = new Timer();
        timer.schedule(task, splashDelay);
    }
}

基本上,logcat 一直在继续,红色波浪的顶部表示“正在停止未恢复的活动:{yo.laststage/yo.laststage.MainPage}”

【问题讨论】:

  • 错误提示MainPage有问题。
  • 感谢泰恩。我已经检查了清单和布局(只是一个文本视图)。还能是什么?
  • 我的意思是,看起来MainPage 已经停止并且之前没有恢复。您的代码显示MainActivity。您未发布的代码可能存在问题。

标签: android android-intent sharedpreferences splash-screen


【解决方案1】:

我有太多的编码并像这样简化了一切。 如果有人感兴趣,我想要实现的是在 MainActivity.class 中有一个 SPLASH 页面,并检查它是否是第一次运行应用程序。 如果是,应用程序会自动重定向到 One.Class ELSE(也不是第一次),它会自动重定向到 Two.Class。

别忘了在 Manifest 中声明你的活动

MainActivity.class:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{

                Boolean isFirstRun = getSharedPreferences("PREFERENCES", MODE_PRIVATE).getBoolean("isfirstrun", true);
                if (isFirstRun){

                    Intent intent = new Intent(MainActivity.this, One.class);
                    startActivity(intent);

                    getSharedPreferences("PREFERENCES", MODE_PRIVATE).edit().putBoolean("isfirstrun", false).commit();
                }
                else{
                    Intent intent = new Intent(MainActivity.this,Two.class);
                    startActivity(intent);
                }

            }
        }
    };
    timerThread.start();




}

@Override
protected void onPause() {

    super.onPause();
    finish();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2017-05-17
    相关资源
    最近更新 更多