I'm trying to recreate() and activity on Android. I need additional data to be passed via an Intent to the recreated activity. How can this be done?
The following code does not work.
getIntent().putExtra("flag",true);
recreate();
You can call recreate() and save your date into SharedPreferences. In this way you are free to do many things and also the previous activity is completely destroyed before NewActivity begin. Take a look at this deep StackOverflow answer.
Why don't you restart activity via Intent :
Intent intent = getIntent();
intent.putExtra("your","params");
finish();
startActivity(intent);
Related
We can use addBackStack(null) with Fragments and in this way the fragment won't open when we press back. But how we can do this with Activity? I could'n find an usage about that.
I also tried this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
but this is not working.
And finally addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) is working but when I take application to background and reopen it this activity is destroyed.
I understand there is no need for Flags. The problem was solved with adding finish() when open other activity.
Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();
startActivity(i);
I know this is probably extremely simple, but I just can not figure it out.
I'm trying to reload/recreate an activity after an action. I know I could just use:
Intent intent = getIntent();
finish();
startActivity(intent);
But in reading through answers on the site I'm told to use 'recreate()' after 11 api. Any help would be appreciated, thanks!
While using the recreate method works by doing
this.recreate()
It was only added in API level 11. If you want to include more devices you can check the API level and implement both the recreate method as well as
Intent intent = getIntent();
finish();
startActivity(intent);
You can use both by making an if statement like...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//Code for recreate
recreate();
} else {
//Code for Intent
Intent intent = getIntent();
finish();
startActivity(intent);
}
this.recreate() is all it takes. Stick that code inside a method that lives in the activity you want to reload. I have a project where this is tied to a button click but you can use it however you need to.
I'm a bit confused by your question, you yourself answered the question in your answer. Call the method recreate directly...
From the documentation for recreate():
Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.
Call recreate() from within the activity code instead of
Intent intent = getIntent();
finish();
startActivity(intent);
to restart the activity (after API 11 that is).
See this answer for a more generic recreate routine that works even for before API/SDK 11.
I am creating a new intent like this:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
And I want to override its onCreate or onPause methods. How can I do it?
thanks.
An Intent is compared to Intent filters as they're definded in the Apps Manifest's. If an Intent filter matches, the corresponding Activity or Service is started.
So an Intent itself has no onCreate or onPause method.
The best you can do is, to create an Activity with the required behavior, put a SurfaceView with Camera logic in it and start it with your own intent.
Here is the link to Androids guide to camera apps. It's not too difficult to write the necessary Activity.
If you start a new intent you ask the system to call any activty that is registered for this action/namespaces. If you yourself register this in your app you can of course change the onCreate() or onPause(), but usually you have no possibility to change how the intent is handled by the other app. Furthermore I do not get your use-case, if you maybe make clear what you want to do SO could be more of assitence.
Form Activity Login I call
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
From Activity BrActivityList I Call
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("BRinfo", objBR);
And Now in BRInfoActivityList
Intent i = getIntent();
BrInfoEN objBR=(BrInfoEN)i.getSerializableExtra("BRinfo");
Now i want if(intent==BrActivityList)The last two above line execute otherwise does not.How to do it?
If you will start activity with
startActivityForResult().
Then you can use getCallingActivity() to get caller.
Form Activity Login call this--
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
intent.putExtra("call_from","ActivityClass");
From Activity BrActivityList Call--
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("call_from", "BRActivity");
Now, when you get the intent, compare the "call_from" string in an if-else block! That's it!
May this help you...
The new intent comes as part of onNewIntent(Intent). The original Intent is still available via getIntent().
You put whatever code you need to into onNewIntent in order to update the UI with the new parameters; probably similar to what you're doing in onCreate.
Also, you probably want to call setIntent(intent) in onNewIntent to make sure future calls to getIntent() within the Activity lifecycle get the most recent Intent data.
Hopefully the title wasn't to confusing but what I meant was the following:
Lets say activity A starts activity B by calling:
Intent myIntent = new Intent(Activity_A.this, Activity_B.class);
Activity_A.this.startActivity(myIntent);
Could I save/free up some memory by finishing Activity_A after Activity_B is begun (if thats even possible). Maybe through the following:
Intent myIntent = new Intent(Activity_A.this, Activity_B.class);
Activity_A.this.startActivity(myIntent);
Activity_A.finish();
Or would Acitivty_A call startActivity() and wait for Activity_B to finish before it called finish()?
The idea would then be that when the users end with Activity_B, it would just restart Acitivity_A (and finish itself in a similar fashion)? Would this create too much overhead? Thanks for any answers and I apologize if the formatting of this post isn't correct.
What you are trying to do is platform's job. You shouldn't really care about optimizations like this.
Still, if you do, this will work just fine:
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
finish(); // finishes this
This will not necessarily save any memory, and I'm not immediately sure of any way to do this. When you go from one activity to another, the first activity is closed and its state bundled. It's not really open anymore at that point.