我正在开发一个包含 SplashScreen.java
我的第一个 Activity 的应用程序。之后显示 LoginActivity.java
用于登录。我的 LoginActivity.java
类最终启动了 SplashActivity.java
。每次我启动我的应用程序 SplashScreen.java
时,我都希望在第一次登录后调用 SplashActivity.java
而不是 LoginActivity.java
。为此,我在我的 SplashScreen.java
类中做了一些更改,但它不能正常工作。
SplashScreen.java 类-
public class SplashScreen extends Activity
{
private long splashDelay = 5000; //5 seconds
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false))
{
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
}
else
{
Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
TimerTask task = new TimerTask()
{
@Override
public void run() {
finish();
Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class);
startActivity(mainIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
有没有人帮帮我。现在是第一次运行后唯一的问题。应用从 SplashActivity
开始,而不是从 SplashScreen
开始。
最佳答案
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
Editor ed = pref.edit();
Boolean Exist=pref.getBoolean("activity_executed", false);// Check is user logged in or not
if(Exist)// if allready logged in then forward it to Splash Activity
{
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
}
else // Not logged in
{
Handler handler = new Handler();
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 2000L);
}
Runnable runnable = new Runnable() {
public void run() {
new AsyncParsing().execute();
}
};
private class AsyncParsing extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.d("Splash Activity", "In pre execute");
}
@Override
protected Void doInBackground(Void... params) {
Log.d("Splash Activity", "In do in backgriund ");
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.d("Splash Activity", "In post execute");
Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
}
关于java - 如何在第一次运行后永远跳过第二个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176518/