I am new to Android and have a question about presenting an activity.
I am creating an Intent using the constructor Intent(context, MyActivity::class.java) and presenting it calling startActivity(intent)from an activity.
The problem is my Activity has a delay before displaying the first frame (it is Flutter if you are curious), and I have a method on the activity instance that notifies me when the first frame is ready, It is called onFlutterUiDisplayed().
Is there a way to make the intent only be actually presented after the first frame callback has been called?
Thanks in advance!
When your second activity goes to foreground, its onResume() method is called.
When this happens, your first activity's onPause() is called.
Check the official documentation to get a better understanding of activity's lifecyle on Android:
https://developer.android.com/guide/components/activities/activity-lifecycle
Related
I want my activity the recognize when it is left and a new activity is started so for example when i'll do
startActivity(intent);
It will perform a certain code.
I tried using onPause();
But it only work on leaving the activity manually
Why don't you perform the operation before calling the new startActivity() and go read about the android activity life cycle to understand how it works
EDIT::
if i truly understand what you are saying..then i think you are looking for onStop() method
Look at the Android activity lifecycle here. onPause is called when "another activity comes to the foreground". If I understand your question correctly, it seems like overriding your first activity's onPause method will work. It's called when you add a new activity on the back stack or when you send the app to the background.
For convenience, here's the chart from the link I provided that I refer to for activity lifecycle questions:
I am making an application in which i want to let the control go to some activity and then to come back. When I have searched about that problem, I came to know that this problem could be solved by using OnPause() and OnResume(), but I don't know how to use them.
This is the code where I am transferring the control to some other activity. Please tell me how to use OnPause() and OnResume() in this regard.Thanks in advance.
Intent intent = new Intent(MCQ.this,ConfigActivity.class);
int ClassIndex = 2;
intent.putExtra("ClassIndex", ClassIndex);
startActivity(intent);
You are not making too much sense. When you call startActivity(), Android will put your current Activity to the background, and start (or bring to foreground) the new Activity specified in your Intent.
onPause() called when your Activity disappears from the screen, eg. the system either puts it in the background, or terminates it.
onResume() is called when your Activity becomes visible on the screen, eg. either after onCreate() if Android created a new instance of your Activity, or when the system brings your Activity back from the background.
There is no explicit passing of control between Activities. If it is on the top of the activity stack (visible on the device screen), than you can interact with it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
onPause() and onResume() are called by android.
Your job is to implement these methods. onPause and onStop are called at your launching activity by Android, and onCreate, onStart and onResume at the Activity your starting. If you want to go back, you usually just press the back button at your device.
You might want to take a look at the Android devGuide and especially at the Activity lifecycle.
I think you may be confused as to what onPause() and onResume() are used for.
onResume() is called when the Activity is about to show it's view to the user. You can use your intent in this method so the main Activity would instantly go to the ClassIndex activity. However, when you leave the ClassIndex activity, onResume() will be called again which will send you back to ClassIndex.
onPause() is called when the Activity is leaving. Whether this is for locking the screen or moving to another Activity. In this case, when you call startActivity in the main Activity, Android will call onPause() in the main activity before entering the new one.
What you may want in this case is to use startActivityForResult() and use it to determine if you're coming back from your ClassIndex activity or not. If not, then start it. If so, then move on to the main activity.
A better solution may-or-may-not be to start the app in ClassIndex and move to your main activity when the user is done with it. This would be the case if you want to move to ClassIndex every time the user enters the app.
In my Activity onCreate method I create and Intent (say to launch the camera) and call startActivityForResult. The problem is that onCreate is called twice and the Intent is launched twice. Both are received in onActivityResult.
What is going on here? How should I automatically launch an Intent when my Activity loads? I tried calling startActivityForResult in onStart, but it is still called twice.
Thanks.
onCreate is normally called when you return from another activity, like in your example. The activity lifecycle docs by Google are a bit misleading in this respect (they make you think onCreate only called once during the app lifecycle).
Your best bet is to save your state in onSaveInstanceState, e.g. add a cameraCalled flag, and then check that flag in onCreate to prevent a loop.
onCreate may and may not be called when you are returning.
It will depend on memory situation and whether or not OS killed your activity. You will need to account for both scenarios. It is probably not doable when you call from onCreate. See this for more information on order of what is called on the return State of Activity while in onActivityResult question
so i've been trying to get my application to run an Activity via an intent and it works fine, when i then assign the finish(); method, it returns to the activity that called it. The only thing i don't understand is that i'm not sure if the callee Activity is put onPause while the called Activity is in-front. I've tried to setup a toast message in the onPause() method of the callee Acitivty but it won't appear.
I first tried to call the second Activity with startActivity(intentname) and then a finish() method on the first Acitivty, i then tried to use the startActivityForResult() (even though i don't really need to recieve any information from the called Activity) method and closed it with onActivityResult().
I can't find any information about the side-effects that these Activity methods has on a Activity that's calling another. So i'm wondering if anybody could help me out ?
//Thx in advance
According to the documentation for Activity, the onPause() lifecycle method WILL be called when another Activity is put in front of it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If the called Activity is is semi-transparent, then onStop() will also be called, but if your initial Activity is not visible at all, onStop() will not be called.
It is also of worth to note that when you call finish() on the called Activity, the onResume() will be called on the caller (and onStart(), assuming onStop() was also called)
To quickly answer your question: if activity A starts activity B, then A's onPause method is run. I think there might be an exception if B isn't full screen, but that's only a tentative memory from something I read in the documentation a while ago.
As for why your toast wasn't showing - did you remember to .show() it? I always used to forget to do that. Toasts can also get missed if they're triggered just as the activity is pausing, since its context goes away. There's a much easier way to test it - just use the Log method. For example, Log.d("My app name", "onPause was just triggered"); The purpose of the "My app name" string is to let you filter by it in LogCat. If you don't know how to display LogCat, and assuming you're using Eclipse, see this answer to another question.
got it to work , was a bit confused of the purpose with onResume(), i was suppose to decleare a onActivityResult() in the first Activity so that the second Activity would return to it right after finish()
I am confused, and need to get my concepts straight.
After executing the last statement, which function is called, in MapsActivity? is it onResume? and under which function (onResume()?) should i put getExtra()?
Log.i("onMenuAnimate", "Attempting to animate to:");
Intent intent = new Intent(SearchDB.this, MapsActivity.class);
intent.putExtra("com.gpsdroid.SearchDB.Lat", nameLatitude.getText());
intent.putExtra("com.gpsdroid.SearchDB.Long", nameLatitude.getText());
SearchDB.this.startActivity(intent);
Take some time to read up on Activity Life cycle; trust me it will help you a lot.
Under the given circumstances, when you call startActivity(..), MapsActivity will be first started by the Activity Manager. In an activity's life cycle, onCreate(..) is called whenever an activity is first created. So this could be one of the places that you can call getExtra().
AFAICT, you can call getIntent.getXXXExtra() in any of the Life Cycle Methods. The answer regarding which of the life cycle methods to choose depends on what is being passed and where/when the information is to be used.
after this statement the next activity which is going to be called.then whenever the back button is pressed the a\first activity will be resumed. the code you want to execute you should put it in overrided method onResume.