How to kill Intent data - android

I'm writing an application that has several activities. The main activity can receive data via Intent, then start another activity that process that data.
I have a problem when finished the second activity, since the main activity runs the onResume method (where check and intentions of the process), and the original intention is still alive. So App calls the second activity again.
My question is to eliminate the Intent data after calling second activity, and thus the return of second intention not to repeat the cycle.
Thank you very much

set a class variable to 0 and then make it 1 instead.
So you don't do the looping.

Can I eliminate the Intent data after calling second activity
yes.
As soon as you call second activity(Or the second activity is started), first activity onPause method will be called. You can override onPause method in Activity1 and do what ever you like to do.
Ex:
#Override
protected void onPause() {
super.onPause();
getIntent().removeExtra("YourMainActivityData");
}

Try add this code after u start second activity in onResume:
setIntent(new Intent());
this will replace the return intent when u call getIntent();
Hope it helps.

I can't remove Intent data, so I wrote an alternative method. I did set the intent-filter at the second activity, and this activity does all the work. When user wants to go back, I start first activity and finih second one.
Perhaps it's a good method, perhaps not is. But it runs OK for me.
Thank you very much!

Related

Why is onCreate called twice but no onDestroy called between two onCreate in my Activity?

My team wrote a complex LBS/chat App, there is a MainActivity as main displaying activity.
When this MainActivity started, "onCreate" is called the first time.
when I switch to other several Apps and go back to my App's MainActivity, "onCreate" is called second time.
But "onDestroy" of MainActivity has never been called.
Some pointers(Singleton/Global, not belong to any activity) will be "auto" set to null out of my control, it causes my app problem.
Does some module/class hold a reference to MainActivity or some other reasons?
I need help -_-!
Thanks to all guys who can give me some tips.
Thanks!
OnCreate is call only once in activity circle life.
If your application was in background and return to foreground, the only reason that on create call again is because OS kill this activity.
you can save data with onSaveInstanceState().
when you start activity this functions be called: onCreate() , onStart() , onResume() and when you going to another activity, onPause() called.
If you want to call onDestroy() to your app you must call this.finish(); but, when you call this.finish() you must handle onBackPress() in your app.
I hope this useful to you

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

killing an activity in android

Since my class could not inherit Activity class, I used context.startActivity() to launch a new activity. In this type of launch, how can kill the launched activity.? How can i use finish() in this circumstance?
If you started a new activity to get some result then you should use startActivityForResult() instead of startActivity().
and if you want to stop an activity after performing some necessary tasks then call finish() at the end of the onCreate() after performing everything necessary.
and if you have something else in your mind then let me know. I will try my level best to help you.
In this type of launch, how can kill the launched activity.?
You don't.
How can i use finish() in this circumstance?
You don't. One activity does not finish another activity in general.
May the very next statement is to kill it.
Since the new activity will not have been displayed by the time of "very next statement", then you should not have started it in the first place.
You have to just pass contex object from LAUNCHER activity's onCreate method.
You may use below code.
`public Class ABc{
Contex co;
ABc(Contex con){
co=con;
}
con.startActivity(intent);
}`
And onCreate method create object of this ABc class.

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

Finish sub activities programmatically

I have an activity that launches another activity with startActivityForResult method. I would like to terminate the called one programmatically but I don't know how to do this since in onActivityResult() method I have no information about the called activity and I cannot call finish() on it. How can I achieve this?
Thanks
The launched Activity can finish self:
setResult(RESULT_OK);
finish();
Try finishActivity(requestCode). According to the documentation it lets you finish an activity previously started with startActivityForResult. And if there are multiple such activities with the same request code, all will be finished.
Note: I haven't actually tried this myself, but that's what the documentation says! Experiment with that, see if it does what you want.
At the moment you call startActivityForResult your Activity will be closed or paused and the new activity is started. The only one that can finish the new activity is the new activity.
You could start a background task and let this background task somehow notify your activity the activity could now finish itself.
I don't know if a Handler created in Activity A and passed to a thread will remain valid if Activity A is paused and Activity B is active. But I would assume this works because both of the Activities are running in the same thread therefore they should share the same message queue.
This thing just bit me, so I thought I'll add a comment here:
if(readyToFinish()){
finish()
}
thisCodeWillBeExecuted()
My experience is that all your code in the stacktrace gets executed, before
the activity is finished. The documentation is not ideal at this point.

Categories

Resources