Recreate android activity without blink - android

Whenever I call recreate() in the android activity. The activity blinks and recreated. Is there any way to avoid this blink effect?

Unfortunately, I clearly don't understand what you meant so:
If you want to restore data when recreate() call, I suggest you to
use [Mvvm].
If the problem is something else, please put your code here.

Related

Android: recreate() doesn't clear EditTexts

I'm actually using recreate() method to restart an Activity, but this method doesn't clear the EditTexts inside the Activity.
How can i solve this?
If you just want to clear your EditText without recreate the entire activity, you should use setText() method to clear it properly.
Like this:
yourEditText.setText("");
As per the documentation for recreate, the call to recreate Cause the 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.
So basically recreate() doesn't actually act the same way as totally recreating the activity.
For example: if you have any Fragments with setRetainInstance(true) they won't be recreated; merely paused and resumed.
One more catch with recreate API is it is supported from API Level 11 and Above. Hence, use of recreate is ok if your app is only targeting SDK level 11 and above.
Check if you are using the setRetainInstance(true) in your code.
You need to may be show more code to understand the specific problem!

How do you recreate an activity within itself without saving its instance?

I want to use recreate() to relaunch my activity, but I don't want it to execute the onSaveInstanceState(). So, it's really like, launching a new activity.
On this page, an answer says that:
Since API level 11 (Honeycomb), you can call the recreate() method of the activity (thanks to this answer).
The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.
Is there any other way to relaunch an activity within itself without calling onSaveInstanceState()?
If you consider this bad practice, what do you think I should do?
have a look here.
onSavedIstanceState and onRestoreInstanceState are always called, but if you don't implement them they will recreate the app without doing anything :)
EDIT: you can add a new save to them, like an int.
when you reload but you don't wanna restore anything, you set this value for example at 1.
when you reload and you want to preserve it you set it for example at 0.
than in onRestoreIstanceState you check for this value, and if it is 1 you don't call any of reload calls, if it is 0 you call them :)
This may not be good way, but the way I handled is reusing the intent that starts the activity.
Define an intent intentOLD in the onCreate method and use
intentOLD = getIntent() to retrieve the Intent that starts this activity. Then when you want to restart the activity, call finish(); startActivity(intentOLD);

Saving State With Android

I have an app that will never require more than one instance of an activity. I want it so that when the user comes back to a screen it is in the same state as they left it except for a few places where it doesn't make sense. I've worked out saving the persisted data with onpause onstop updates. However to keep the screen looking the way it did when they left it i use intents specifically setting the flags to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_SINGLE_TOP then startActivity. It seems to work great but does it make sense? Is there a smarter way? Pitfalls doing it this way etc... any feedback will be greatly appreciated.
android:launchMode = "singleTask"
add the above line for every activity in the manifeast file. Adding these launch relaunch the activity instead of creating the activity again.
Refer this link

How do I refresh Activity like it is being loaded for first time?

I am trying to figure out how, when a user lands on my activity screen, it is "reloaded" as if it were being loaded for the first time.
I don't want my user hitting the back arrow and coming back to my activity with old information.
As it is now, when a user "comes back" to my page, the database list isn't being repopulated, and information they typed into EditText fields remains there.
I want the page, everytime the user comes to it, to be like it's their first time there.
Have you ever tried using recreate() in your current activity? Try using it after your new values are populated.
Or you can just put everything that's in the onCreate() on the onResume. A bit ugly but it works.
I have to disagree with Kartik on this, as I understand, android:noHistory='true' will remove activity from application stack. So when user hits back, user will not be able to see the activity at all.
About activity not retaining its value, I would not recommend you this, as user expects that all values would be retained when back is hit, unless there is some specific requirement that you are trying to meet.
So I guess solution to your problem is, as others have suggested do your initialization on views in onResume(). But just doing this may not be sufficient, as views like EditText will by default cache the values anyways. So you might have to manually clear those in your on onResume(). Will keep looking to find any 'perfect' solution if any to this problem.
I had solving similar problem like yours and I solve it whit lunch mode.
I think the best is to take a look of this set the lunch mode in single instance and then
in you onCreate and onResume you can code to refresh you view just the way you want it.
copy requred code into onResume() from onCreate() method

Android app exit, which Mechanism to use?

following are the ways to exits from the application
1. ActivityObject.finish();
2. Runtime.getRuntime().exit(0);
I want to know which way is to be used & when ?
if there is another way please let me know
Thanks in advance.
Shrenik
That's usually not a good idea at all to "exit" an application in android. That's against Android nature. Read this topic first before doing something like that.
Look at this life cycle of an Android activity:
And the description of the OnDestroy state:
The final call you receive before your
activity is destroyed. This can happen
either because the activity is
finishing (someone called finish() on
it, or because the system is
temporarily destroying this instance
of the activity to save space. You can
distinguish between these two
scenarios with the isFinishing()
method.
So calling ActivityObject.finish() is the right way to do it.
call moveTaskToBack(true) on your Activity

Categories

Resources