Android: recreate() doesn't clear EditTexts - android

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!

Related

Recreate android activity without blink

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.

I have a trouble in Fragment life cycle and need a resolution to come out of it

I have done ample research on this, and there is not one clear solution on the problem.
In the life-cycle, particularly in the Fragment life-cycle, following Exception comes any moment after onPause().
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
My logic says, that to continue with the current fragment, after it reaches this state, I have to restart the activity and again point back to the intended fragment using Intent.
I want to be clear on what is happening and what should be real solution to deal with it.
I need to know the pros and cons of this mechanism; its importance in Fragment or Activity life-cycle.
Also, if I am changing the Windows Feature in onCreate to not to go to sleep, unless if the user has manually pressed the home button, will still the activity will go to this state?
This exception happens when you're trying to add/remove/replace/interact in any other way with a Fragment inside the Activity when it's paused.
Which means Activity will not be able to restore it's state (restore the state of a Fragment which has been changed) if it will be destroyed right away.
Best solution here, is to check that Activity is NOT paused during the interaction with a Fragment.
Another option is to use commitAllowingStateLoss() to interact with Fragment transaction, with a risk of losing it's state.
See:
https://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()
In a perfect world you should analyze each crash carefully and add checks to verify that you interact with fragments only when Activity is up and running.
A better explanation is presented in a new Android developer reference and guide documents for using JetPack Life Cycle Listener.
https://developer.android.com/topic/libraries/architecture/lifecycle#kotlin
The library makes the components Activity Life Cycle aware. That means you do not require an abstract baseActivity class which overrides every life cycle callback, and record that state in a boolean variable. LifeCycle listener will do it for you.
All you have to do is stop introducing a new fragment or stop any Loader that updates the UI when its response returns. The right time to do this is before onStop or onSavedInstance state is called, and your components will be made aware of it.
It clearly states that after the onSavedInstancState or onStop is called the UI becomes immutable till the onStart of the Activity is called again. Sometimes you have to call restart the same activity using NEW TASK and CLEAR TASK flags using intent, when this state occurs and there is no chance that otherwise onStart is going to be called.
Happy Coding :-)

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

How to fully refresh android app after configuration change

I want that my activities are recreated from scratch when device cofigs like font size changes or app is launched after killed by OS. What's the best way to achieve this? By default lifecycle methods like onCreate etc are called but activity is not fully initialized.
Simply call the recreate method of the Activity:
recreate();

Access GUI elements before it is created?

In Activity lifecycle the onActivityResult method is called before the onCreate methode, where all my buttons etc. are initialized.
How is it possible to access them though? Thx for some background knowledge!
The only way to an onActivityResult event should happen is if you used startActivityForResult to create the new activity that generates that event.
Which means your calling activity should be in the stack and already created but just paused so your onCreate was already called.
Unless there is an orientation change in between which I have never tried and poses an interesting question.
Are you having an issue or just trying to understand the mechanism? If you are having and issue post some code.

Categories

Resources