Basically, this is what I'm doing
1) Set AlarmManager to execute BroadcastReceiver (BCR)
Intent intent = new Intent(m_Context, BCR.class);
intent.putExtras(extras);
PendingIntent pendingIntent = PendingIntent.getBroadcast(m_Context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, StartTime, pendingIntent)
2) Start MyActivity from BCR
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent(context, MyActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
3) Have MyActivity turn on the screen if its not on
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.myactivity);
}
#Overide
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
For some reason, I notice that right when MyActivity is opened, it's flow goes like:
onCreate/onNewIntent -> onResume -> onPause -> onResume
I'm not sure why it does an onPause right away. I notice this only happens when the screened is being turned on by the flags. Does anyone know why this happens? Is there any way this behavior can be prevented?
if you trying request permissions every time it can cause such problems, just check if you already granted them
requestPermissions can cause it:
onCreate
onStart
onResume
onPause
onResume
Use this method ContextCompat.checkSelfPermission(context, permission) to check if permission was granted or not before requesting it
This method returns int and you can check it with PackageManager.PERMISSION_GRANTED constant
Just in case anyone else runs into this, I seem to notice this behaviour only when I inflate fragments inside of an activity via XML layout. I don't know if this behaviour also happens with the compatibility library version of Fragments (I'm using android.app.Fragment)
It seems the activity will call Activity#onResume once before calling Fragment#onResume to any added Fragments, and then will call Activity#onResume again.
Activity:onCreate
Fragment:onAttach
Activity:onAttachFragments
Fragment:onCreate
Activity: onStart
Activity: onResume
Fragment: onResume
Activity: onResume
If you have ES File Explorer then FORCE STOP it. Somehow, they interrupt your app's lifecycle (comments suggest some kind of overlay).
My issue with onResume being caused twice was because onPause was somehow being called after the activity was created.. something was interrupting my app.
And this only happens after being opened for the first time after installation or built from studio.
I got the clue from another post and found out it was because of ES File Explorer. Why does onResume() seem to be called twice?
As soon as I force stop ES File Explorer, this hiccup behavior no longer happens... it's frustrating to know after trying many other proposed solutions. So beware of any other interrupting apps like this one.
I was researching about this for a while because on the internet there is no any mention about this weird behaviour. I don't have a solution how to overcome this dark-side-behavior but I have found an exact scenario when it certainly happens.
onPause-onResume-onPause-onResume just happens every time, when app is starting first time after installation. You can simply invoke this behavior by doing any change in code and rerunning (which includes recompiling) the app from your IDE.
No matter if you use AppCompat libs or not. I have tested both cases and behavior carries on.
Note: Tested on Android Marshmallow.
I have borrowed the code from this thread about fragment and activity lifecycle and here it is (just copy, paste, declare activity in manifest and run Forest run):
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestActivity extends Activity {
private static final String TAG = "ACTIVITY";
public TestActivity() {
super();
Log.d(TAG, this + ": this()");
}
protected void finalize() throws Throwable {
super.finalize();
Log.d(TAG, this + ": finalize()");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, this + ": onCreate()");
TextView tv = new TextView(this);
tv.setText("Hello world");
setContentView(tv);
if (getFragmentManager().findFragmentByTag("test_fragment") == null) {
Log.d(TAG, this + ": Existing fragment not found.");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(new TestFragment(), "test_fragment").commit();
} else {
Log.d(TAG, this + ": Existing fragment found.");
}
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, this + ": onStart()");
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, this + ": onResume()");
}
#Override
public void onPause() {
super.onPause();
Log.d(TAG, this + ": onPause()");
}
#Override
public void onStop() {
super.onStop();
Log.d(TAG, this + ": onStop()");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, this + ": onDestroy()");
}
public static class TestFragment extends Fragment {
private static final String TAG = "FRAGMENT";
public TestFragment() {
super();
Log.d(TAG, this + ": this() " + this);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, this + ": onCreate()");
}
#Override
public void onAttach(final Context context) {
super.onAttach(context);
Log.d(TAG, this + ": onAttach(" + context + ")");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, this + ": onActivityCreated()");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, this + ": onCreateView()");
return null;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, this + ": onViewCreated()");
}
#Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, this + ": onDestroyView()");
}
#Override
public void onDetach() {
super.onDetach();
Log.d(TAG, this + ": onDetach()");
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, this + ": onStart()");
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, this + ": onResume()");
}
#Override
public void onPause() {
super.onPause();
Log.d(TAG, this + ": onPause()");
}
#Override
public void onStop() {
super.onStop();
Log.d(TAG, this + ": onStop()");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, this + ": onDestroy()");
}
}
}
I don't know for sure what's going on, but I suspect that your activity is being restarted because setting the screen on is treated by the system as a configuration change. You might try logging the configuration on each call to onResume to see if that's what's happening and, if so, what is actually changing. You can then modify the manifest to tell the system that your activity will handle the change on its own.
protected void onResume() [
super.onResume();
Configuration config = new Configuration();
config.setToDefaults();
Log.d("Config", config.toString());
. . .
}
I have similar problem.
My situation was next
CurrentActivity extends MainActivity
CurrentFragment extends MainFragment
I was opening CurrentActivity with intent as usually. In onCreate CurrentAcitivity I was replacing CurrentFragment.
Life Cycle was:
1. onResume MainActivity
2. onResume CurrentActivity
3. onResume MainFragment
4. onResume CurrentFragment
called onPause Automatically, and after that again
onResume MainActivity
onResume CurrentActivity
onResume MainFragment
onResume CurrentFragment
I decide to retest everything and after few hours spend trying and playing I found root issue.
In MainFragment onStart I was calling startActivityForResult every time (in my case android popup for turning on Wifi) which was call onPause on MainFragment. And all of us know that after onPause next is onResume.
So its not Android bug, it's only mine :-)
Happy lifecycle debuging!
I also ran into this onresume-onpause-onresume sequence (on 4.1.2 and above, but I did not experience this on 2.3). My problem was related to wakelock handling: I accidentally forgot to release a wakelock and reacquiring it caused an error with a message "WakeLock finalized while still held". This problem resulted in onPause being called immediately after onResume and resulted in faulty behavior.
My suggestion is: check for errors in the log, those might be related to this issue.
Another hint: turning on the screen might be a bit more tricky than simply using window flags. You might want to check this answer here - it suggests you set up a receiver to check if the screen has already been turned on and launch the desired activity only after: https://stackoverflow.com/a/16346369/875442
I had a similar issue, and my problem was that at the onCreate() method, I was doing:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.friends); <-- problem
}
My call to "super." was triggering the onResume() twice. It worked as intended after I changed it to just:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.friends); <-- 'super.' removed
}
Hope it helps.
Have you tried calling your getWindow().addFlags(...) before calling super.onCreate(savedInstanceState) in onCreate method?
I had a similar problem. onResume was called twice when my onCreate looked like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
setContentView(R.layout.main);
...
}
Changing it to:
#Override
public void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
}
... fixed the problem.
It seems that using Activity from the support library saves and restores instance automatically. Therefore, only do your work if savedInstanceState is null.
I just ran into this, and it seems that getWindow().addFlags() and tweaking Window properties in general might be a culprit.
When my code is like this
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generic_fragment_host);
// performing fragment transaction when instance state is null...
onResume() is triggered twice, but when I remove requestWindowFeature(), it's only called once.
I think you should have a look at that question:
Nexus 5 going to sleep mode makes activity life cycle buggy
You should find leads
Basically a lot of stuff can trigger this. Some resume processes that loses focus can do it. A few apps will cause it to happen too. The only way to cope is to block the double running. Note, this will also have an errant pause thrown in for good measure.
boolean resumeblock = false;
#Override
protected void onResume() {
super.onResume();
sceneView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
sceneView.getViewTreeObserver().removeOnPreDrawListener(this);
if (resumeblock) return false;
resumeblock = true;
//Some code.
return false;
}
});
}
This is a solid way to prevent such things. It will block double resumes. But, it will also block two resumes that preserve the memory. So if you just lost focus and it doesn't need to rebuild your stuff. It will block that too. Which might be a benefit clearly, since if you're using the resume to control some changes over focus, you only actually care if you need to rebuild that stuff because of focus. Since the pre-draw listeners can only be called by the one thread and they must be called in sequence, the code here will only run once. Until something properly destroys the entire activity and sets resumeblock back to false.
as #TWL said
ES File Explorer
was the issue for me !
Uninstalling the app solved the problem.
When this ES File Explorer was installed, onStart() -> onResume() -> onPause() -> onResume() .. was the problem.
onResume() was called 2'ce.
I had the same problem. Mine was for this code in runtime
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
I just put it in manifest
android:screenOrientation="landscape"
no more problem about twice call to onCreate and onResume.
I didn't see an adequate answer to the question, so I decided this.
From My Notes:
"It seems that the activity runs onResume() onPause() then onResume again. The very last method that runs is the onSizeChanged(). So a good practice would be to start threads only after the onSizeChanged() method has been executed."
So you could: Make a log of each method that runs. Determine the last method that runs. Ensure that you have a Boolean that initializes false, and only changes to true after your last method runs. Then you can start all threading operations, once you check that the Boolean is true.
-For anyone wondering: I am using a surfaceview that has a onSizeChanged() method that executes very last.
I had the same problem because of setting the UiMode in the onCreate() of MainActivity. Changing the theme triggered activity recreation and made two calls to onPause() and onStart().
I was sure this was happening in my app until I realized I had planted two of Jake Wharton's Timber trees, and onResume() was just being logged twice, not called twice.
i also faced this issue this is because of fragments..
the number of fragments you have in activity onResume() will call that number of times. to overcome i used flag variables in SharedPrefrences
Related
Im trying to make an alarm clock using flutter, and start the flutter activity with a path to the alarm ("/alarm" path). I have acccomplished starting the MainActivity using MethodChannels, but i need to somehow route to "/alarm", but calling getFlutterView().pushRoute("/alarm") does not do anything. The activity just starts in the main view instead of alarm route.
Thanks in advance!
I have managed to startActivity using MethodChannels, but when calling getFlutterView().pushRoute("/alarm") in onCreate, it does not change the route.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
// This does not change the route (setInitialRoute doesn't work either)
getFlutterView().pushRoute("/alarm");
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
#Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("setAlarm")) {
// pushRoute works here, but not in onCreate
getFlutterView().pushRoute("/alarm");
} else {
result.notImplemented();
}
}
}
);
}
Expected results: Change the route to "/alarm"
Actual results: Nothing happens, the activity gets opened on initial route eg. the main page
Found the solution,
pushRoute() or setInitialRoute() doesn't work until the View has been inflated.
Here is the code that works:
FlutterView.FirstFrameListener mListener = new FlutterView.FirstFrameListener() {
#Override
public void onFirstFrame() {
getFlutterView().pushRoute("/alarm");
}
};
getFlutterView().addFirstFrameListener(mListener);
I want to make a cloud synchronization everytime my app is brought to front and a second time if the app disappears in background.
So I overwrote the onStart and onStop event methods of my activity:
#Override
protected void onStart() {
super.onStart();
doSync();
}
#Override
protected void onStop() {
doSync();
super.onStop();
}
Ok, that works fine for me but I found out that these methods are also called if I start a new activity (f.e. SettingsActivity.class) within my app (onStop) and come back to the main activity (onStart).
Is there a good way to ignore the calls of my own activities and only react on calls from "outside", f.e. I only want to synchronize if the user stops the app by pressing the home button and I also want to synchronize only if the user returns to the app by starting it from the app dreawer or app switcher?
+++ SOLUTION +++
Now I found a solution for my problem and I want to share it. Maybe it's not the best way because it's no SDK-based functionality but it works and it's quite simple.
I declared a flag, set it to false when the activity is created. Everytime I start another activity in the same app, I will set the flag to true and check its state in onPause and onResume.
public class MainActivity extends Activity {
private boolean transition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
transition = false;
}
private void startSettingsActivity() {
transition = true;
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
private void doSync() {
// all steps for the cloud synchronization
}
#Override
protected void onResume() {
super.onResume();
if (!transition) {
// this is the case the user returns from
// the app drawer or app switcher or starts
// the app for the first time, so do sync
doSync();
} else {
// this is the case the user returns from another
// activity, so don't sync but reset the flag
transition = false;
}
}
#Override
protected void onPause() {
if (!transition) {
// this is the case the user presses the home button or
// navigate back (leaves the app), so do final sync
doSync();
} else {
// this is the case the user starts another activity, but
// stays in the app, so do nothing
}
super.onPause();
}
}
I have a situation with onResume() method and I don't know how to solve it.
conssider the following code:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle b) {
super.onCreate(b);
Log.d("tag", "onCreate");
}
#Override
protected void onResume() {
super.onResume();
//do something only when everytime the activity comes to screen
Log.d("tag", "onResume");
}
private void myMethod() {
this.onResume();
}
}
If we asume that myMethod will definitly be called, I don't want to let onResume() to execute //do something only when everytime the activity comes to screen. I should note that myMethod is a fixed method and cannot be changed.
PS:The reason that I am asking this question is that I have a simillar situation with PermissionDispatcher library with android 6 and I want to call a "risky permission" in the onReume() method but if the user denies the permission, it will call the onReume() again, and since the permission required task is in the onResume(), the permission will be denied again and cauases an inifite loop
could anyone give me a suggestion?
UPDATE: here is the permissionDispatcher library and the issue that is related to my problem
verify this method Already Executed or not simply in if Condition
Bool a=true;
#Override
protected void onResume() {
super.onResume();
//do something only when everytime the activity comes to screen
if(a==true)
{
//your Actions
}
else if(a==false)
{
//do nothing
}
}
Use SharedPreferences in onResume
#Override
protected void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean firstStart = settings.getBoolean("firstStart", true);
if(firstStart) {
//do something only when everytime the activity comes to screen
Log.d("tag", "onResume");
//display your Message here
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstStart", false);
editor.commit();
}
}
Couldn't find a solid answer to this anywhere. I have a method where finish() is being called, and onPause() is called afterward.
Is onPause() guaranteed to be called after a call to finish() ?
Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate().
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finish();
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
Run, and observe that your log will only contain "onDestroy". Call finish() almost anywhere else and you'll see onPause() called.
The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). so
onPause() is guaranteed..The official documentation here
EDIT 1
onCreate() is to onDestroy() && onStart() is to onStop() && onResume() is to onPause() .. onStart() is called when onCreate() finishes its work. if not its not called.. onResume() indicates the ui is about to be shown to the user -(An Activity's content is the screen the user sees).
if you call finish() in onCreate(), onPause() will be skipped because onResume() was never called same goes to onStart() .. so in some cases you can say its not; but that will be false, because what's an Activity that is not a screen or serve as a container for screens-(Fragment).
and why would you call finish(); directly in your Activity's onCreate()? From how Activities work in general, onPause() will always guarantee is calling..
Yes, whenever you activity is going to be disappeared, onPause is called.
It is a bit late but may help future visitors.
I faced the problem of onPause() not being called when I tried to make an splash screen for my app following this tutorial.
After some fiddling around and forcing my mind I figured out why! the onCreate() method looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
So before the activity gets a chance to get started or resumed I was forcing it to be finished and so bypassing the other state method calls!
But as far as I know from the official Activity documentation in any other situation (at least normal ones!) the onPause() method is guaranteed to be called!
I have been reading the answers in other post about this topic, but i have not found what is the best of all the approaches.
This is my approach i have now, but i do not know if it worst all the time (as far as i tested every worked for my) or if there is a better way.
public class FatherClass extends Activity {
private static int activities = 0;
public void onCreate(Bundle savedInstanceState, String clase) {
super.onCreate(savedInstanceState);
}
protected void onRestart()
{
super.onRestart();
if(activities == 0){
Log.i("APP","BACK FROM BACKGROUND");
}
}
protected void onStop(){
super.onStop();
activities = activities - 1;
}
protected void onStart(){
super.onStart();
activities = activities + 1;
}
}
Explanation: the onStart is executed one the activity is "visible" and the onStop when the activity is "not visible". So when your APP (it says APP not activity) goes to background all the activities are "not visible" so they execute the onStop method, so the idea behind this is to add one each time an activity es started, and subtract one each time an activity es hided, so if the value of the variable "activities" is 0 "zero" that means that all the activities that where started in some point are now not visible, so when you APP returns from background and executes the onRestart method on the activity in "front" you can check whether comes from background or is just restarting an activity.
I would appreciate some feedback regarding this topic.