Android Splash活动在第一次设置后不重定向

问题描述:

我有一个设置活动,我只想在第一次安装时显示。Android Splash活动在第一次设置后不重定向

然后我有一个加载启动画面,5秒后重定向。

但是,当我保存了我的设置活动后,它会转到启动页面,但启动页面会卡住并且不会重定向。

在设置被看到后,所有其他的启动都能正常工作。它只是在第一次启动它是越野车。

请帮忙吗?

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainactivity); 
     // get shared preferences 
     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); 
     // first time run? 
     if (pref.getBoolean("firstTimeRun", true)) { 
      // start the preferences activity 
      startActivity(new Intent(this, Settings.class)); 
      //get the preferences editor 
      SharedPreferences.Editor editor = pref.edit(); 
      // avoid for next run 
      editor.putBoolean("firstTimeRun", false); 
      editor.commit(); 
     } else { 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); 
        startActivity(mainIntent); 
        finish(); 
       } 
      }, 5000); 
     } 
    } 
} 

这是因为Activity的生命周期。当您从设置活动返回时,不会再次调用onCreate。要解决,你可以简单地重写的onResume()和移动

// get shared preferences 
     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); 
     // first time run? 
     if (pref.getBoolean("firstTimeRun", true)) { 
      // start the preferences activity 
      startActivity(new Intent(this, Settings.class)); 
      //get the preferences editor 
      SharedPreferences.Editor editor = pref.edit(); 
      // avoid for next run 
      editor.putBoolean("firstTimeRun", false); 
      editor.commit(); 
     } else { 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); 
        startActivity(mainIntent); 
        finish(); 
       } 
      }, 5000); 
     } 

里面(去除的onCreate整件事)

+0

所以,我该怎么办?对不起,我很困惑你的意思 – Bimal 2015-02-07 13:05:26

+0

移动片段内的onResume。这就是全部 – Blackbelt 2015-02-07 13:06:00

+0

哪里是onresume哈哈对不起有点初学者 – Bimal 2015-02-07 13:07:04