2013-06-12 60 views
0

我有一個litlte代碼添加一個快捷方式到主屏幕的第一進行時間:如何創建主屏幕快捷方式恢復頂部活動

Intent shortcutIntent = new Intent(getApplicationContext(), 
      SFlashActivity.class); 

    shortcutIntent.setAction(Intent.ACTION_MAIN); 

    Intent addIntent = new Intent(); 
    addIntent 
      .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "New App"); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
      Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
        R.drawable.ic_launcher)); 

    addIntent 
      .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    addIntent 
      .putExtra("duplicate", false); 
    getApplicationContext().sendBroadcast(addIntent); 

但隨着上面的代碼,我的應用程序總是開始閃屏althought我的應用程序是運行。 那麼我怎麼能讓主屏幕快捷方式恢復到頂級活動。 我注意到,由谷歌在安裝上發揮的應用程序的快捷方式總是恢復頂級活動。

非常感謝!

+0

你應該在第一次啓動應用程序時將數據存儲在「SharedPreferences」中。 –

+0

如果您的應用程序已在運行,則標準行爲是恢復頂級活動。如果沒有發生,你會遇到一些奇怪的事情。我知道你已經接受了一個答案,但這個答案是矯枉過正的,不應該是必要的。將您的清單的內容添加到問題中。那裏可能有些討厭的東西。 –

+0

另請注意,如果應用程序最初是從安裝程序或IDE(Eclipse,Android Studio等)啓動的,則Android中存在一個錯誤,會顯示您描述的行爲。爲確保您沒有看到此錯誤:請在設備上安裝您的應用程序,請勿通過單擊安裝程序屏幕上的「打開」將其打開,現在請轉至主屏幕並通過單擊應用程序圖標啓動您的應用程序。看看你的問題現在是否已經消失。請參閱http://stackoverflow.com/questions/11296203/on-click-of-shortcut-on-homescreen-launching-from-spalsh-screen-in-android –

回答

1

使用isTaskRoot()方法 輸入以下代碼片段到你的OnCreate()的主要活動 這裏有一個例子:


@Override public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     setContentView(R.layout.splashscreen);    
     if(!isTaskRoot()){ 
      finish(); 
      return; 
     } 
} 

找到了解決辦法here

1

當通過主屏幕上的圖標啓動時,除非應用程序已在運行(在這種情況下顯然會恢復堆棧頂部的活動),否則Android始終會使用AndroidManifest.xml中的android.intent.action.MAIN篩選器啓動該活動。

爲了實現你所說,你可以簡單地將最後一個可見活性存儲在SharedPreferences並有調度活動,根據喜好開始最後一個活動是什麼。

因此,如果活動存在於prefs中,則啓動該活動或啓動SplashScreen。

在每一個活動要自動重新啓動:

@Override 
protected void onPause() { 
    super.onPause(); 

    SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE); 
    Editor editor = prefs.edit(); 
    editor.putString("lastActivity", getClass().getName()); 
    editor.commit(); 
} 

而一個調度活動類似以下內容:

public class Dispatcher extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Class<?> activityClass; 

     try { 
      SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE); 
      activityClass = Class.forName(
       prefs.getString("lastActivity", SplashScreen.class.getName())); 
     } catch(ClassNotFoundException ex) { 
      activityClass = SplashScreen.class; 
     } 


     startActivity(new Intent(this, activityClass)); 
    } 
} 

備註

  • 你可以爲創建一個基類覆蓋
  • 調度活動顯然需要的android.intent.action.MAIN行動

參考: - How to make an android app return to the last open activity when relaunched?

+0

感謝您的回答!我會盡力回答你的問題 – redwind

+0

:)。樂意幫助你。 :) –

+0

我仍然想知道爲什麼通過谷歌播放快捷鍵總是恢復頂級活動? – redwind

0

我還通過恢復應用程序,而回來時,應用程序是在同樣的問題去由於主頁按鈕背景,

兩個改變要做

1.將以下屬性添加到清單文件中的活動

android:alwaysRetainTaskState="true" 

這將恢復來自啓動器圖標的活動。

2.如果您點擊已經以編程方式創建的主屏幕上的應用程序圖標,則上傳不會恢復應用程序。因爲您已經爲啓動目的指定了「SFlashActivity.class」。爲了克服這個問題,你必須做一個如下的技巧:

添加此功能到你的SFlashActivity。類

public boolean CheckIfAppIsResumable(){ 
    try{ 
     ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); 
     List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1); 
     Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator(); 
     while(itr.hasNext()){ 
      RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next(); 
      CharSequence desc= runningTaskInfo.description; 
      int numOfActivities = runningTaskInfo.numActivities; 
      int numRunning=runningTaskInfo.numRunning; 
      String topActivity = runningTaskInfo.topActivity.getClassName(); 
      Log.d("description", ""+desc); 
      Log.d("numActivities", ""+numOfActivities); 
      Log.d("numRunning", ""+numRunning); 
      Log.d("topActivity", ""+topActivity); 
      if(numRunning>1 && topActivity.equalsIgnoreCase("com.yourpackage.yoursplashclass")) 
       return true; 
     } 
     return false; 
    } 
    catch(Exception e){ 
     Log.d("Errror CheckIsAppIsResumable=", ""+e.getMessage()); 
     return false; 
    } 
} 

而且在OnCreate中:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    if(CheckIfAppIsResumable()){ 
     finish(); 
     return;//will finish the new started splash activity 
    } 

只要你的快捷方式的意圖沒有標誌FLAG_ACTIVITY_CLEAR_TOP。

相關問題