2015-10-04 95 views
1

上下文:有一個toggleButton在「Record」和「Stop」之間切換。點擊記錄時,會創建一個通知,告訴用戶後臺服務正在運行,即使用戶已經點擊主頁按鈕等並移出應用程序。點擊通知後,用戶應該能夠進入活動並「停止」錄製並保存文件。無法恢復點擊通知的活動狀態

問題:如果我點擊的記錄(以及由此切換按鈕),然後按下home鍵,然後單擊該通知,它涉及到的活動,但切換按鈕是「記錄」而不是'停止'。即它沒有保留先前的狀態。我認爲這是因爲創建了一個新的活動實例,而不是重用現有活動實例。

代碼:

public void showRecordingNotification(){ 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
     mBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     mBuilder.setContentTitle("Recording in progress!"); 
     mBuilder.setContentText("Go to the app to stop."); 
     Intent notificationIntent = new Intent(this, MainActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent appPendingIntent=PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(appPendingIntent); 
     Notification notification= mBuilder.build(); 
     notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 

     mNotificationManager.notify(NOTIFICATION_ID, notification); 
    } 

Android清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="space.wavicle.sensorvector2d" > 

    <uses-feature 
     android:name="android.hardware.sensor.accelerometer" 
     android:required="true" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name"> 
     <service android:name=".DataLogger"/> 
     <activity 
      android:name=".MainActivity" 
      android:launchMode="singleTask" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".ReplayActivity" 
      android:label="@string/title_activity_replay" 
      android:parentActivityName=".MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="space.wavicle.sensorvector2d.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 

我已經試過什麼:周圍的Googling和狩獵周圍是這樣,我發現有類似的冠冕堂皇的問題很多人,所有的解決方案似乎圍繞意圖的標誌,其中我嘗試了所有有意義的組合。我也有清單中的android:launchMode="singleTask"(也嘗試過android:launchMode="singleInstance")。他們似乎都沒有做這項工作。

回答

4

在我看來,你應該問自己一個不同的問題。

爲什麼這個按鈕很重要,如果這個activity是一個新的實例呢?

你究竟知道你的錄音狀態?如果你在活動中保存狀態,這意味着如果活動以某種方式丟失(崩潰)或死亡,則會失去你的狀態。那很糟。

你說你有一個服務在後臺運行,我會讓服務保持我的狀態。

編輯:

爲實例的問題,首先,這裏是到谷歌文檔的鏈接,所以你可以進一步閱讀了有關該主題:

http://developer.android.com/guide/components/tasks-and-back-stack.html#ManifestForTasks

總之

,有4 launchModes狀態,標準,singleTop,singleTask,singleInstance。 你可以閱讀文檔中的差異。

我看到它的方式,我會建議你去「singleTask」狀態。

基本上,在singleTask中,如果您通過通知再次打開該活動,例如它將恢復當前已打開的活動,您可以調試它並看到您不會進入通常的onCreate,您將去通過onResume(和OnNewIntent的..)

+0

這使得很多道理 - 服務來判斷記錄按鈕應該打開還是關閉。我會盡力實施。然而,不應該有一種方法來通過點擊通知來獲得應用程序的保留狀態嗎?我肯定我必然遲早需要 – sudP

+0

我編輯了我的答案,更多的信息:) – JozeRi

+0

我一直在使用singleTask。但是我沒有重寫NewIntent()。這可能是問題嗎? (我正在閱讀你現在發送的指南) – sudP