Additional actions to restore activity

My application contains MainActivity and uses full-screen fragments to display content. I try to achieve the following when I re-create the application (when the application has gone in the background for a long time, it will be killed by the system and then it will be brought to the fore).

  • If the user manually re-creates the application (selecting the application from the list of recent applications), the main activity should recreate, and then the last fragment, which was full-screen, should recreate. There is no problem, this is standard behavior.

  • If the application is re-created because the user has touched the push notification, the main activity should be recreated, but the last fragment that was in full-screen mode should not be recreated. Instead, a new fragment should be created and displayed . What for? The push notification contains information about which fragment should be displayed

My approach is based on checking the additional settings that I add when creating a notification that launches the main activity. This part of the code is not shown for brevity, but I have always added some additional features to the intent, always

MainActivity.java

@Override protected void onCreate(Bundle savedInstanceState) { //debug System.out.println("MainActivity.onCreate() extras:"+getIntent().getExtras()); if (savedInstanceState!=null && getIntent().getExtras()!=null){ //case 2. passing null will prevent Fragment recreation super.onCreate(null) } else{ //case 1. super.onCreate(savedInstanceState) } ... } @Override public void onResume(){ //debug System.out.println("MainActivity.onResume() extras:"+getIntent().getExtras()); ... } 

Now, if the application does not start at all and a notification arrives, I get the following output:

 MainActivity.onCreate() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData.... MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData.... 

When the application is manually created by the user, I get the following output:

 MainActivity.onCreate() extras: null MainActivity.onResume() extras: null 

When the application is re-created by notification

 MainActivity.onCreate() extras: null MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData.... 

This last case is not what I expected, as additional add-ons for notification are only available after calling super.onCreate ()

Any ideas on how to achieve 1. and 2 ..

Giant editing: This is a notification code, as you can see, there is a splash screen activity that conveys the intention of the main activity.

GcmIntentService.java

 @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { sendNotification(extras.getString(KEY_CATEGORY), extras.getString(KEY_CONTENT)); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } private void sendNotification(String category, String content) { category = evaluateCategory(category); mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Intent mIntent = new Intent(this, SplashActivity.class); mIntent.putExtra(KEY_CATEGORY, Integer.valueOf(category)); mIntent.putExtra(KEY_NEW, true); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.valueOf(category), mIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.logo_actionbar) //customize notification mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(Integer.valueOf(category), mBuilder.build()); 

SplashActivity.java

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent mIntent = getIntent(); if (mIntent.getExtras()!=null){ category = mIntent.getExtras().getInt(GcmIntentService.KEY_CATEGORY); isNew = mIntent.getExtras().getBoolean(GcmIntentService.KEY_NEW); } final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); mainIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP); if (category!=null ){ mainIntent.putExtra(GcmIntentService.KEY_CATEGORY, category); mainIntent.putExtra(GcmIntentService.KEY_NEW, isNew); } startActivity(mainIntent); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); finish(); } }, SPLASH_DELAY); } 
+5
source share
1 answer

What about overriding the OnNewIntent method (intent intent) provided by Activity?

protected void onNewIntent (intent intent)

This is called for actions that set startMode to "singleTop" in their package, or if the client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity (Intent). In any case, when the activity is restarted while at the top of the action stack instead of a new instance of the operation to be launched, onNewIntent () will be called in the existing instance with the intention that it was used to restart it.

Activity will always be paused until a new intent is received, so you can count on onResume () called after this method.

Note that getIntent () still returns the original intent. You can use setIntent (Intent) to update it to this new intent.

+2
source

Source: https://habr.com/ru/post/1201193/


All Articles