2013-10-04 191 views
1

我正在處理一個包含SplashScreen.java我的第一個活動的應用程序。之後,它顯示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開始。

+1

你是什麼意思'它不是SplashActivity後fine.'工作 –

+0

@geet我有QuestionActivity的意思。在SplashActivity中,我有退出按鈕,可以將控制權交還給Splashactivity。在Splash Activity傳輸控制中的退出按鈕以在QuestionActivity中退出退出按鈕,即遞歸方式。 –

+0

@geet我知道它的littlt混亂,請讓我知道,如果你不明白。 –

回答

1

@約翰檢查,只是指這個工作的代碼,我希望它幫U,

setContentView(R.layout.main); 
    pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE); 
    Log.v("","onCreate is calling"); 
    if(pref.getBoolean("activity_executed", false)) 
    { 
     Log.v("","Before if called"); 
     setContentView(R.layout.menu_frame); 
     Log.v("","after if called"); 
     new Handler().postDelayed(csRunnable1, 3000); 

    } 
    else 
    { 
     new Handler().postDelayed(csRunnable2, 3000); 
     Editor ed = pref.edit(); 
     ed.putBoolean("activity_executed", true); 
     ed.commit(); 

    } 


} 
Runnable csRunnable1=new Runnable() 
{  
    @Override 
    public void run() 
    { 
     Intent intent = new Intent(SplashScreen.this, SplashActivity.class); 
      startActivity(intent); 
      finish(); 

    } 
}; 
Runnable csRunnable2=new Runnable() 
{  
    @Override 
    public void run() 
    { 
     Intent intent = new Intent(SplashScreen.this, LoginActivity.class); 
      startActivity(intent); 
      finish(); 

    } 
}; 
+0

經過如此多的修改後,您的答案完全正常。需要你多一點幫助。當你在這裏背文本。謝謝您的回答。 –

+0

@JohnR歡迎! – patrioit

0

我認爲你有一個邏輯問題。如果不執行,則運行它並標記爲已執行。

if(!pref.getBoolean("activity_executed", false)) { 
    Editor ed = pref.edit(); 
    ed.putBoolean("activity_executed", true); 
    ed.commit(); 
    Intent intent = new Intent(this, SplashActivity.class); 
    startActivity(intent); 
    finish(); 
} 
+0

不能正常工作遞歸問題閱讀下面的問題。 –

0

問題是調用finish並不會停止執行onCreate的其餘部分。你需要把一個return語句後:

Intent intent = new Intent(this, SplashActivity.class); 
startActivity(intent); 
finish(); 
0
// try this 
    @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(); 
      Timer timer = new Timer(); 
      TimerTask task = new TimerTask() 
      { 

       @Override 
       public void run() { 
        timer.cancel(); 
        Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class); 
        startActivity(mainIntent); 
        finish(); 
       } 

      }; 

      timer.schedule(task, splashDelay); 
     } 

    } 
+0

我試過了你的代碼。但下次我的應用程序從SplashActicity開始而不是從SplashScreen開始。 –

0

你失蹤我希望這將解決您的問題

public class SplashScreen extends Activity 
{private long splashDelay = 5000; // 5 seconds 

    /** Called when the activity is first created. */ 
    TimerTask task = new TimerTask() { 

     @Override 
     public void run() { 
      finish(); 
      Intent mainIntent = new Intent().setClass(Test.this, 
        LoginActivity.class); 
      startActivity(mainIntent); 
     } 

    }; 

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



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




     } 

    } 
} 
+0

我試過了你的代碼。但下次我的應用程序從SplashActicity開始啓動,而不是從SplashScreen –

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

步驟1:在烏爾登錄類聲明這個

SharedPreferences pref; 
SharedPreferences.Editor editor; 

步驟2:聲明在onCrete方法

pref = getSharedPreferences("login", MODE_PRIVATE); 
editor = pref.edit(); 

步驟3:的OnClick的在登錄頁面提交按鈕粘貼下面的線 :

String check=pref.getString("selected", "nil") 
if(check.equals("TRUE")) 
{ 
Intent intent=new Intent(this,splash.class); 
startActivity(intent); 
} 

第4步:將此處放在用戶credentails和db憑證匹配時獲取成功消息的地方。

editor.putString("selected", "TRUE"); 
editor.commit(); 
+0

請使用@patrioit回答進行更改。那麼你的應用完美。謝謝你這個代碼莫妮卡的偉大。 –

+0

偉大的檢查這個http://stackoverflow.com/questions/4182761/finish-old-activity-and-start-a-new-one-or-vice-versa –

相關問題