How to call recreate()? - android

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.

Related

Best way to handle activity state

I'm making an app that shows another activity when I correctly login however in the home screen when I pressed back and then returned to the app the login activity showed.
I was able to avoid this overriding the onBackPressed this way:
public void onBackPressed()
moveTaskToBack(true);
}
Is this the best way to do it? Is there a more proper way to keep the state of the application when I exit it?
In your login activity after starting intent call finish()
finish destroy your activity and avoid to run it again automatically.
Your issue is that you kept LoginActivity in stack. so when you press back it will kill MainActivity(after login) and since LoginAcitivity still there. it will be shown again. best is to kill LoginAcitivty after starting new activity.
call this in LoginActivity:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();
As you question I understand, you want to clear the entire history stack and start a new activity. For this, in API level 11 a new Intent Flag was added which is: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
For API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. So you can support pre API level 11 as well.

Include Intent extra when using recreate()

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

Android 2.2 (API 8) Logging Out

I have an app that will have multiple activities open.
Activity A -> Activity B -> Activity C
I want to logout and close all open activities. I have read many different links that describes how to do this.
On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites
Close all running activities in an android application?
The issue for this however is that the Intent Flags only work with API level 11 and above. I have an app that I would like to be made available for API level 8 and above. I know that it goes way back but what is the best way to accomplish this for API level 8?
Or should I just give in and make the minimum level 11?
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);``
Did you try this ?
void signOut() {
Intent intent = new Intent(this, HomeActivity.class);
intent.putExtra("finish", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities *not necessary*
startActivity(intent);
finish();
}
from this answer
Okay, you can do one thing get ProcessID of your application and in onDestroy() kill that process. Thats it from this link How to force stop my android application programmatically?
int pid=android.os.Process.myPid();
android.os.Process.killProcess(pid);
you can use alternative told by one of user here using startActivityForResult() and onActivityResult() works fine even on API level 8.
For this u need to use
startActivityForResult() instead of startActivty. and onActivtyResult() for you work.
you say "I want to logout and close all open activities." , Intent.FLAG_ACTIVITY_CLEAR_TOP works on API Level 8 , I have incorporated Logout functionality ,that way in app that is already in store, but there is one difference if I understand you correctly , logout goes to LoginActivity , logout does not kill process or something as this goes against concept of Android. Anyway you can even do this also when you do this as suggested above:
void logout() {
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("finish", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
And then in LoginActivity you can search for "finish" boolean in extra in onCreate method and finish that activity too.
#Override public void onCreate(Bundle state) {
super.onCreate(state);
if (getIntent().getBooleanExtra("finish", false)) finish();
}

Get calling activity from some other activity

I am calling an activity from another activity by this code:
Intent intent = new Intent(context, mClass);
context.startActivity(intent);
and from my new activity which is started by this code. I want to know which activity starts this activity. I used this code for this purpose
Intent intent = getIntent();
Class<?> c = intent.getClass();
if (c != OffersActivity.class) {
prepareForNotifications();
setListAdapter();
}
but by this code I am not able to get the classname which starts this activity. I really need some help.
thanks
There is a method getCallingActivity(), but that only works, if the calling activity calls you with startActivityForResult(). I have seen libraries use that and say that you must call them that way, but frankly it is a bit nasty. The simple answer is that you're not supposed to know. Android is a system of loosely coupled activities and the calling activity should thus have no real meaning to your activity. Any information your activity needs should be put explicitly in the intent, so if you really want to know who called you then the caller needs to put that extra in. You won't be able to tell if they are lying of course, so don't go trying to use this for anything security-related. However, I would suggest that whatever you're determining based on the caller, is what the caller should be passing you instead. Might be time for a rethink on why you want to do this, since trying to fight the system will only lead to pain.
I would suggest:
public static final String INTENTSENDER = "sender";
void mMethod() {
Intent intent = new Intent(context, mClass);
intent.putExtra(INTENTSENDER, mClass);
context.startActivity(intent);
}
knowing who sent it:
Class<?> c = (Class<?>)intent.getExtras().get(INTENTSENDER);
However, you can also use this:
ComponentName componentName = this.getCallingActivity();
Now, you can use componentName to get the sender of the intent.
Maybe it's best to use the extras parameters in the intent when you call them...like: Intent.putExtra(PARAM,value) on the caller activity...and on the opened activity you check:
intent.getStringExtra(PARAM)
getParentActivity() is not what yout are looking for?

Is it possible to free up memory by finishing an activity after calling another?

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.

Categories

Resources