My application works fine, once it is initialized in the OnCreate method of my View class. However, when I open my app after the Droid phone has been sitting idle all night, the OnCreate method is not being called.
I use the OnCreate to initialize data, and that in turn initializes the GUI. The GUI clearly shows that OnCreate was not called.
I tried setting clearTaskOnLaunch="true" in my Manifest. My Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hedgetools.trin"
android:versionCode="2"
android:versionName="1.02">
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:clearTaskOnLaunch="true">
<activity android:name=".Trin"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="6" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
This did not help. My OnCreate method is not being call after the Droid phone sits idle all night.
Why doesn’t clearTaskOnLaunch cause OnCreate to be called?
Any help or suggestions will be greatly appreciated.
Charles
OnCreate gets called only when the application is launched as a fresh one. Subsequently the OnResume function gets called.
See http://developer.android.com/reference/android/app/Activity.html for a description of the application life cycle.
clearTaskOnLaunch tells android to remove any activities other than the main one from the history stack when you launch it from home. it has nothing to do with triggering onCreate.
Sidharth has it right, for that you need to put your initialization in onResume, not onCreate. see the diagram in http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle for a very quick explanation of the different activity states.
Related
I have 3 major class in the application
1) Intent service: where I receive push notification and open activity according to notification message and other two classes behavior. below is the code which does that
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 the mediator screen so if the application is not running this screen will be shown first and on click of yes button of this screen MainScreen will be opened and this screen will be finished.
3) Main screen: This is the main screen of the application which show map. its core behavior is that ts a launchmode="singletask" mentioned in menifest file, which means if this screen is running its hole data will be sent to onNewIntent() method rather than opening this screen again.
Now what is happening in flow is
Step 1: application is in background and push notification comes. condition run and the second condition gets success and notification screen intent is shot
step 2: In notification screen I click on ye button to move on to the next main screen
step 3: In Main screen I process this info and perform task or just close the application
Step 4: again a new notification is received and as the application is not running is goes to second condition and start the intent for notification screen but this time no notification screen is launched instead of providing its intent and main screen is launched which is wrong.
This is the abnormal behavior which I am facing that instead of providing class of notification screen for intent main screen is launched which is totally different behavior of application according to android.
Any help from any one who come across such problem will 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 in which I am testing is "YU Yureka" here is its specification link. Current it have android 5.0.2 OS
Third Edit
To test this behavior I have debugged the code from eclipse debugger. To check this I have put a break point in NotificationScreen onResume and onCreate but it was never hit instead of onResume of MainScreen is hit.
Also I added a logs in if and else condition but still logs for else condition is printed.
Fourth Edit
Global.isMainScreenRunning: is global boolean variable which is done false in onPause of MainScreen and done true in onResume of MainScreen.
Global.NotificationScreenRunning: is global boolean variable which is done false in onPause of NotificationScreen and done true in onResume of NotificationScreen.
Global state is evil, and this question demonstrates why. To understand what's happening, you'll have to look at every place where your global flags are set or cleared, and analyze all the possible execution paths that might lead there. That's way beyond the scope of the kind of help you can get on SO. And there's an extra complication on Android, since the entire VM might be destroyed (and all your globals cleared) any time your app is in the background. Just as bad, the VM and the Application instance might be retained after you exit the last activity. When that happens, Application#onCreate(...) will not be called again the next time you start the app, and all your static globals will still be set to whatever they were when you last ran the app.
Save yourself a lot of hair-pulling and stop using global state.
You can try add android:taskAffinity=":main" to your MainScreen activity in manifest.
Maybe because the MainScreen still in a same task with the NotificationScreen so it's was call up all the task when NotificationScreen called again.
You can try this demo for more visual:
play app and code
Hope it help.
As I got your point you want to resume Notification Screen every time when you get notification in the app.For this I can suggest a solution this will make the application runs and allows you to achieve expected behavior but when you press home button your Main screen will no longer active.It will be killed.As we all know android maintains activity stacks for all acticities.So if you press device back button then activity will be poped out from stack and you will not face this problem.
I think this is a standard behavior of activity stacks for more information you can go with this link:
http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
You can try this code snippet:
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();
Hi #Abhinbav Singh Maurya
I think the problem is that main screen is getting called from your splash screen declared in the manifest as the application launch screen, so when the app is killed and you are calling the intent this will get called.
Instead of that what can you do is broadcast the message to the splash screen and receive it in splash screen and based on condition call the intent from there.
Again here there is a thing that you should take care of, you have to broad cast the intent only after the receiver is registered in the SplahScreen, else it will never be received. so try to user handler.postDelayed method and broadcast 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 have answered a similar question in the following thread.
here
I think this is what you are looking for. Please let me know if this met your requirement or not, if you find the answer useful, if my answer helps you, up vote the answer so that it will be useful to others.
Cheers :)
Edit
I have did a bit research on it.
You can have two launcher activities according to this link.
Try it and let me know. Hope this helps you. :)
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);
}
This is a strange behavior of Android Lollipop 5.0.
I have two activities, A and B.
A starts the activity B. When I click on the back button, in activity B, Android calls onCreate method on A.
This behavior is observable only in Lollipop 5.0.
In the other versions, onCreate is never called after finishing another activity.
What is the problem?
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<application
android:name=".Application"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppThemeMaterial" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".activities.ActivityA"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.ActivityB"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/activityB" >
</activity>
</application>
</manifest>
please make sure that option not selected
Developer Options -> Don't keep activities
What I think is happening is that when you leave the Activity A, onStop() is being called on activity A since its completely hidden and B is on top of it.
Usually now when you resume activity A after pressing back onStart() is called and then onResume().
But, if you see the Activity LifeCycle, it is technically possible for onCreate() to be called as well, in the case where your app's process is killed by the system if other apps with higher priority need more memory.
I'm having an issue starting activities in order, and I don't know if it is an issue in the manifest or in the code. I tested this code a while ago when it was working, but now it's not.
The first activity links to the second, which links to the third. I listed the first activity first in the manifest. However, when I start my emulator, it's the second activity that runs first. I am very confused. Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hmdywifinal.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Activity1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"
android:label="Startpage">
</activity>
<activity android:name=".Activity3"
android:label="Activity3"></activity>
</application>
Do you think something is wrong with it?
Make sure you are running your program from Activity1 and not from Activity2.
If you run it from Activity2 it will skip Activity1 even though you have your manifest set like you described above.
Order in which Manifest file declares Activities has nothing to do with the runtime order.
First Activity gets launched from a Launcher ( which is Activity1 in your case )
I am assuming you are launching Activity2 and 3 using Intents in your code. So you are in control on the way these activities get launched.
Refer to the api Demos, Which has got a similar application Proof Of Concept. It will give you a better idea on mving from one application to other.
I'm working on an application that currently has two activites, a log in activity and a "frontpage" activity after that.
Whenever I switch away from the application and resume it either by relaunching it or using the application switcher, it will always re-open the log in activity. Even if I switch to my app from within the frontpage activity, it opens the log in activity.
None of my other apps have ever done this, even though they obviously all have a filter for the launcher in AndroidManifest.xml. I am aware that it is supposed to do this when the activity is killed, but I cannot comprehend why it would do that.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.vertinode.facepunch"
android:versionCode="1"
android:versionName="1.0">
<!-- Android 2.2 -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<!-- Internet access -->
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name=".FPApp">
<!-- Login form -->
<activity android:name="nl.vertinode.facepunch.LoginActivity" android:launchMode="singleTask" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Frontpage view -->
<activity android:name="nl.vertinode.facepunch.FrontpageActivity" android:theme="#android:style/Theme.Light.NoTitleBar" android:windowSoftInputMode="stateHidden">
</activity>
</application>
</manifest>
What could I be doing wrong?
I'm going to guess this has to do with your android:launchMode being singleTask.
Try singleTop instead and see if you have the same problem. Also it could have to do with some code you have in your onPause for the other activities. Does it go back to your Login activity after going to sleep/waking back up as well?
I have 3 Activity in my androidManifest,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zepod.whatelsecomics" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activity1"
android:label="#string/list1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"
android:label="#string/list2">
</activity>
<activity android:name=".Activity3"
android:label="#string/addItemToList2">
</activity>
</application>
The first one call the second one
The second one call the third one
The first one activity is the main activity.
The result of the third one activity is an updated list in the second one (just a form).
But now, if i clik the back button i came back at the third one
How can i force the app to came back at the first activity?
I suppose this depend by androidManifest but i don't understand the intent-filters order
Can someone help me?
When starting to the third activity, call finish() to remove the second activity from the stack. That's the easiest way.
There are other ways. This question has been asked a million times on SO, search for it and you'll find alternatives.