2015-11-08 16 views
0

我有一个Splash活动,其中通过意图,我希望用户根据是否第一次访问该应用程序而被重定向到不同的类。危险的组合Splash +共享首选项

除了是很慢,崩溃

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 /哟.laststage.MainPage}”

+0

的错误表示'MainPage'问题。 – tynn

+0

谢谢Tynn。我已经检查了清单和布局(只是一个textview)。它还能是什么? – MDR

+0

我的意思是,它看起来像'MainPage'已经停止,并没有恢复之前。你的代码显示了'MainActivity'。您没有发布的代码可能存在问题。 – tynn

回答

0

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

不要忘了在清单

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