Question about Intent, android - android

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.

Related

Present an activity after an event

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

android on new activity start

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:

Android launch intent from onCreate

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

Pattern for activity that starts another activity (for result)

I have an activity where the first thing it does is start another activity for result. when the result comes back, it should process it, then finish. I have the startActivityForResult() call in onCreate().
What I see is that sometimes when I return from the target activity i started, onCreate() in my activity is called again. this of course re-starts the target activity a second time.
This makes sense and I understand why this is the case, but I don't understand the correct pattern for what I'm trying to achieve. When I return from the activity i started, i don't want to re-start the target activity again obviously ... I just want to run onActivityResult() and finish.
I read where someone suggested setting a state preference, but that seems like a good source for bugs, for example, if it got stuck in the wrong state.
Any thoughts?
The key is to start the target activity in onResume(), not onCreate(). From the javadocs on onActivityResult(),
You will receive this call immediately
before onResume() when your activity
is re-starting.
In other words, you can be assured that onActivityResult() is called before onResume() ... So for example, set a flag that says "don't start target activity this time" in onActivityResult() so when onResume() is subsequently called, you can avoid re-starting the target activity.

startActivity on Android

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()

Categories

Resources