Applying abnormal behavior does not trigger the activity specified in the intent

I have 3 main classes in the application

1) Intent service: when I receive a push notification and open activity in accordance with the notification message and other behaviors of the two classes. below is the code that does this

if(Global.isMainScreenRunning){ Intent intent = new Intent(this, MainScreen.class); intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else if(!Global.NotificationScreenRunning){ Intent intent = new Intent(this, NotificationScreen.class); intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } 

2) NotificationScreen: this is a mediator screen, therefore, if the application does not work, this screen will be displayed first, and after clicking the β€œyes” button on this screen, MainScreen will open and this screen will be completed.

3) Main screen: This is the main screen of the application that displays the map. its main behavior is that ts a launchmode="singletask" mentioned in the manifest file, which means that if this screen works, its hole data will be sent to the onNewIntent() method, and not to open that screen again.

Now what happens in the stream,

Step 1: the application is in the background and a push notification appears. the condition is started, and the second condition is successful, and the intent of the screen is removed.

Step 2: On the notification screen, I press the "you" button to go to the next main screen.

Step 3: On the main screen, I process this information and complete the task or just close the application

Step 4: a new notification is received again, and since the application does not work, it goes into the second condition and launches the intention for the notification screen, but this time the notification screen does not start, and its intention does not appear, and the main screen starts, which is incorrect.

This is the abnormal behavior that I came across, instead of providing a notification class screen for the main intent screen, which is a completely different application behavior according to android.

Any help from anyone who encounters such a problem would be greatly appreciated.

Edit

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.front" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="android.hardware.microphone" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.app.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.app.permission.C2D_MESSAGE" /> <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" /> <application android:allowBackup="true" android:icon="@drawable/android_app_icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SplashScreen" android:configChanges="keyboardHidden|orientation" android:label="@string/app_name" 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=".MainScreen" android:configChanges="keyboardHidden|orientation" android:launchMode="singleTask" android:excludeFromRecents="true" android:screenOrientation="portrait" > </activity> <activity android:name=".NotificationScreen" android:configChanges="keyboardHidden|orientation" android:excludeFromRecents="true" android:screenOrientation="portrait" > </activity> <receiver android:name=".pushnotification.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.selebrety.app" /> </intent-filter> </receiver> <service android:name=".pushnotification.GcmIntentService" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest> 

Second edit

The mobile phone I'm testing in is "YU Yureka", this is its specification link . He currently has Android 5.0.2 OS

Third editor

To test this behavior, I debugged the code from the eclipse debugger. To test this, I set a breakpoint in NotificationScreen onResume and onCreate , but it was not deleted, not onResume from MainScreen .

I also added logs to the if and else condition, but still logs the else else condition.

Fourth edit Global.isMainScreenRunning : a global boolean variable that runs in onPause from MainScreen false and onResume in MainScreen in MainScreen .

Global.NotificationScreenRunning : a global boolean variable that runs in onPause from NotificationScreen and runs true in onResume NotificationScreen .

+5
source share
5 answers

The global state is evil, and this question shows why. To understand what is happening, you need to look at all the places where your global flags are set or cleared, and analyze all the possible execution paths that could lead to this. This path goes beyond the help that you can handle. And there is an additional complication on Android, since the entire VM can be destroyed (and all your global flags are cleared) at any time when your application is in the background. Equally bad, the VM and Application instance can be saved after exiting the last action. When this happens, Application#onCreate(...) will not be called again the next time the application is launched, and all your global static characters will still be configured to what it was when you last ran the application.

Save yourself from pulling hair and stop using the global state.

0
source

You can try adding android:taskAffinity=":main" to your MainScreen activity in the manifest.
Perhaps because MainScreen is still in the same task as NotificationScreen, so it called the whole task when NotificationScreen called again.
You can try this demo for a more visual one: play the application and code
Hope this helps.

0
source

As I understand it, you want to resume the notification screen every time you receive a notification in the application. To do this, I can offer a solution that will make the application launch and allow you to achieve the expected behavior, but when you press the home button your main screen will no longer be active. He will be killed. As we all know, the android supports activity stacks for all indicators. So, if you press the return button of the device, the activity will be pushed out of the stack, and you will not encounter this problem.

I think this is the standard behavior of activity stacks for more information, you can go to this link:

http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack

You can try this piece of code:

 Intent i = new Intent(NotificationFullScreen.this, MainActivity.class); i.putExtra("FROM", "Notification"); i.putExtra("bookingId", bookingId); i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND); startActivity(i); NotificationFullScreen.this.finish(); 
0
source

Hi @Abhinbav Singh Maurya

I think the problem is that the main screen is called from your splash screen declared in the manifest as the application launch screen, so when the application is killed and you call the intent, it will be called.

Instead, what you can do is send a message to the splash screen and receive it in the splash screen and, based on the condition, call the intent from there.

Again, there is a thing that you should take care of, you should widely apply the intention only after the recipient is registered in SplahScreen, otherwise it will never be accepted. so try using the handler.postDelayed method and broadcasting after 1000 ms or 700 ms.

  Intent mIntent = new Intent(context, SplashScreen.class); mIntent.setAction(Intent.ACTION_MAIN); mIntent.addCategory(Intent.CATEGORY_LAUNCHER); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mIntent); broadcastMessage(context, message); 

I answered a similar question in the next thread. here

I think this is what you are looking for. Please let me know if this meets your requirement or not, if you find the answer useful, if my answer helps you, try to answer so that it is useful to others.

Greetings :)

Edit

I have worked a bit on this.

You can have two launch activities according to this link.

Give it a try and let me know. Hope this helps you. :)

-1
source
 if(Global.isMainScreenRunning){ Intent intent = new Intent(this, MainScreen.class); intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(Intent);//here is the problem change Intent to intent } else if(!Global.NotificationScreenRunning){ Intent intent = new Intent(this, NotificationScreen.class); intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } 
-2
source

All Articles