Issue
I am implementing a webview loading the URL of an web-application that dynamically adds content to the DOM based on user-action via a socket.
As I am closing the App and a user adds another element in the web-application the app-user gets a notification.
If I re-open the App via manually starting it again, I can easily restore the webview and see the new content as well.
My problem: I want to achieve the same when re-opening the App via the notification. Currently I do not get savedInstanceState in onCreate().
launchMode is set to singleTop
Code in WebAppInterface.java:
System.out.println("WebAppInterface:" + MainActivity.MY_ACTIVITY_STATUS);
if (!MainActivity.MY_ACTIVITY_STATUS) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Neue Nachricht")
.setContentText(username + " hat dir geschrieben!")
.setAutoCancel(true)
.setSound(alarmSound)
.setVibrate(new long[] { 0, 1000, 1000, 1000, 1000 });
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
Intent intent= new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Code for restoring in my MainActivity:
if (savedInstanceState != null) {
myWebView.restoreState(savedInstanceState);
} else {
myWebView.loadUrl(URL_TO_WEB_APP);
}
So far I'Ve testes various combinations of Intent.FLAGS/launchMode as well as saving savedInstanceState to a private static variable and trying to restore the webview based on this variable (getting tons of "nativeOnDraw failed; clearing to background color." in LogCat).
Upon starting the App via notification:
private static variable "myWebView" (in which I store the webview) is set
onCreate() parameter Bundle savedInstanceState is null
private static variable "savedState" (above mentioned attempt to safe savedInstanceState and use it) is set
I'd be happy for every hint and/or even a solution, as I am currently completely clueless.
Solution
Even though I'm sure I had this combination tested dozens of times, the working combination - in order to restore the state of the WebView - is:
In WebAppInterface.java:
Intent intent= new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
In AndroidManifest.xml:
<activity
...
android:launchMode="singleTask" >
Furthermode there was no code needed for onResume()/onNewIntent() in the MainActivity.
Answered By - A.Kalkhoff
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.