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.
Related
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!
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
Please tell me the Difference Between Synchronous Activities ans ASynchronous Activities..
and also about that
-startActivity() is Synchronous or Asynchronous
-startActivityForResult() is Synchronous or Asynchronous
and why..
From first principles, synchronous activities means that Activity A operation will depend on activity B eg(coordinating to work with a shared param) Asynchronous activities means that the operation of two activities are totally disconnected.
According to my general concept both startActvity() and startActivityForResult() are asynchronus as in the synchronus Activity the current activity will depend on the new activity.
For example :
suppose in current actvity:
Intent i=new Intent(First.this,Second.class);
startAcivity(i); //or startActivityForResult(i,0);
Log.v("First","Activity");
Now for any activity to be synchronus the later part of the current acivity (once the new activity is started) must be executed after the onCreate method of the new actvity(here Second class).
But generally this never happens. I mean the later part of the current Activity( once a new acivity is started by calling startActivity() or StartResultForActivity()) is never depended on the onCreate method of the New activity.
for more you can go through these links :
What is the mechanism behind startActivityForResult() in Android?
http://osdir.com/ml/Android-Developers/2009-12/msg04249.html
Hope from the above example you understood exactly what startActivity() & StartResultForActivity() are and what is synchronus & asynchronus Actvity is....:)
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.
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.