save value of a variable in a DialogFragment when the screen rotates - android

Well, I wanted to save value of a variable contained in a dialogFragment when the screen is rotated in Android. I've tried every method I could find on the internet, and none of them has worked for me. Some kill my application, and others simply were not doing anything.
I need a real and effective way to save the value of an EditText that is reset when the device screen rotates. The EditText is in a DialogFragment turn this into a FragmentActivity.
thank you very much

Set your fragment's retaingInstance flag to true:
http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
This prevents Fragment instance from being recreated.
Also be sure you don't recreate the fragment all the time:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
Fragment f = new Myfragment();
f.setRetainInstance(true);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, f))
.commit();
}
}
Since View state is preserved during Activity recreation and your are keeping the same Fragment instance you don't need to save TextView value all the time.

I don't know if I understood your question, but you can capture the rotation as follows:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//save value
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//save value
}
}
If you want that your activity don't restart, you should add this into the manifest file:
<activity
.....
android:configChanges="keyboardHidden|orientation|screenSize"
..... >
</activity>

I just found out that you can not apply the methods to save bundles automatically to the dialogues, but these would apply to activities or fragments of these dialogues dependent. If I can fix it by code, will put the solution here. thank you

Related

Activity componnent messed up when handling configuration Changes myself

I added this code in my activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activityView);
}
And also added this line in the Manifest file
android:configChanges="orientation|keyboardHidden|screenSize"
The activity is not restarting, but the layout is not correctly loaded : not all componnent are showing and buttons click listner is never called ...
I'm using the same name file for all the layouts. but different content depending on orientation.
I even tried with different names : activityView_port/activityView_land and changed the code to:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activityView_port);
} else {
setContentView(R.layout.activityView_land);
}
}
but nothing is working ...
any help would be appreciated :)
setContentView(R.layout.activityView_land);
please dont call above method on configuration change. instead you can try with use some flag for determine what layout has to be set and call reCreate() method.
If don't want to destroy the current instance, better you can try removing and adding of views with animation.

Disable other fragments in auto rotation

i have an app with various fragments and the problem is when the phone rotates, the app displays other fragments from the begining. it does not close the current fragments but looks like layers on top of each other. any help i'd be grateful. thanks
Try this to add in your AndroidManifest
<Activity
....
....
android:configChanges="orientation|screenSize">
You need to check for a savedInstanceState, and if it exists, don't re-create your fragments. just check if is null or not
as shown below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
// Do your oncreate because there is no bundle
}else{
// Do that needs to be done even if there is a saved instance, or do nothing
}
}
check this for more detail.

Android Rotation (Load Layout) data loss (id and data / Custom Objects etc.)

I am turning a view, which is the same but horizontally. which contains many variables of type TextView, progressbar, imageview, etc .. but I can not load the variables when rotated.
I tried to do:
android: configChanges = "keyboardHidden | orientation | ScreenSize | ScreenLayout"
I find methods that can save my customized classes, my progressbar, my views "My nothing."
http://i.imgur.com/A1ugd3W.png
When there anyway to load a landscape (another layout (with the same id's)) of not losing data?
To change the layout I use:
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
Update Code 10/04/2015 (0:45 AM)
My rotation :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_1);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_2);
}
}
Oncreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_1);
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_2);
}
References : Saving Android Activity state using Save Instance State Can't sabe all objects exits of an activity. "I think"
The easiest way would be to not specify orientation for the configChanges. Correspondingly, you will not need to override onConfigurationChanged -- you can simply load the layout in onCreate and state should for the most part be preserved. If needed state data can be stored in onSaveInstanceState (and restored in onCreate).
can you provide your code?
One possible issue is that you might be loading your layout file in onResume instead of onCreate. You need to create the layout in onCreate so the savedInstanceState info gets "injected" back into your app. But again, please provide codes :)
My guess is that elements of your layout don't have the same ID. Also, why don't you just have an XML for certain specifications - one for landscape and one for portrait? I bet Android would behave better if you did that instead of a different name for a different configuration

Handle Fragment duplication on Screen Rotate (with sample code)

There are some similar answers, but not to this situation.
My situation is simple.
I have an Activity with two different layouts, one in Portrait, another in Landscape.
In Portrait, i use <FrameLayout> and add Fragment into it dynamically.
In Landscape, i use <fragment> so the Fragment is static. (in fact this doesn't matter)
It first starts in Portrait, then i added the Fragment by simply:
ListFrag listFrag = new ListFrag();
getSupportFragmentManager().beginTransaction()
.replace(R.id.FragmentContainer, listFrag).commit();
where ListFrag extends ListFragment.
Then i do a screen rotate. I found the listFrag is re-creating in the Landscape mode.
(In which i noticed the onCreate() method is called again with a non-null Bundle)
i tried to use setRetainInstance(false) like #NPike said in this post. But the getRetainInstance() is already false by default. It does not do what i expected as the docs said. Could anyone please explain?
The fragment i am dealing with, is a ListFragment, which does setListAdapter() in onCreate(). So the if (container == null) return null; method cannot be used here. (or i dont know how to apply).
I got some hints from this post. Should i use if (bundle != null) setListAdapter(null); else setListAdapter(new ...); in my ListFragment? But is there a nicer way to indeed removing/deleting the fragment when it is destroyed/detached, rather than doing it in its creation time? (so as the if (container == null) return null; method)
Edit:
The only neat way i found is doing getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.FragmentContainer)).commit(); in onSaveInstanceState(). But it will raise another problems.
When screen is partially obscured, like WhatsApp or TXT dialogue boxes pop up, the fragment will be disappeared also. (this is relatively minor, just visual issue)
When screen rotate, the Activity is completely destroyed and re-created. So i can re-add the fragment in onCreate(Bundle) or onRestoreInstanceState(Bundle). But in the case of (1), as well as switching Activities, neither onCreate(Bundle) nor onRestoreInstanceState(Bundle) will be called when user get back to my Activity. I have nowhere to recreate the Activity (and retrieve data from Bundle).
Sorry I didn't say it clearly that, i already have a decision making, which the getSupportFragmentManager()...replace(...).commit(); line only run in Portrait mode.
Example code
I have extracted the simple code, for better illustration of the situration :)
MainActivity.java
package com.example.fragremovetrial;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.FragmentContainer) != null) {
System.out.println("-- Portrait --"); // Portrait
ListFrag listFrag = new ListFrag();
getSupportFragmentManager().beginTransaction()
.replace(R.id.FragmentContainer, listFrag).commit();
} else {
System.out.println("-- Landscape --"); // Landscape
}
}
#Override
protected void onResume() {
super.onResume();
if (findViewById(R.id.FragmentContainer) != null) {
System.out.println("getRetainInstance = " +
getSupportFragmentManager().findFragmentById(R.id.FragmentContainer).getRetainInstance());
}
}
}
layout/activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
layout-land/activity_main.xml (doesn't matter)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
ListFrag.java
package com.example.fragremovetrial;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
public class ListFrag extends ListFragment {
private String[] MenuItems = { "Content A", "Contnet B" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("ListFrag.onCreate(): " + (savedInstanceState == null ? null : savedInstanceState));
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, MenuItems));
}
}
Notice i got the following
Debug messages
-- Portrait --
ListFrag.onCreate(): null
getRetainInstance = false
(rotate Port -> Land)
ListFrag.onCreate(): Bundle[{android:view_state=android.util.SparseArray#4052dd28}]
-- Landscape --
Previously focused view reported id 16908298 during save, but can't be found during restore.
(rotate Land -> Port)
ListFrag.onCreate(): Bundle[{android:view_state=android.util.SparseArray#405166c8}]
-- Portrait --
ListFrag.onCreate(): null
getRetainInstance = false
(rotate Port -> Land)
ListFrag.onCreate(): Bundle[{android:view_state=android.util.SparseArray#4050fb40}]
-- Landscape --
Previously focused view reported id 16908298 during save, but can't be found during restore.
(rotate Land -> Port)
ListFrag.onCreate(): Bundle[{android:view_state=android.util.SparseArray#40528c60}]
-- Portrait --
ListFrag.onCreate(): null
getRetainInstance = false
where the number of Fragment created will not increase infinitely as screen keep rotating, which i cannot explain. (please help)
The correct way to handle this is to put the following in your onCreate()
if (savedInstanceState == null) {
// Do fragment transaction.
}
I finally come up with a solution to prevent unwanted Fragment re-creating upon the Activity re-creates. It is like what i mentioned in the question:
The only neat way i found is doing getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.FragmentContainer)).commit(); in onSaveInstanceState(). But it will raise another problems...
... with some improvements.
In onSaveInstanceState():
#Override
protected void onSaveInstanceState(Bundle outState) {
if (isPortrait2Landscape()) {
remove_fragments();
}
super.onSaveInstanceState(outState);
}
private boolean isPortrait2Landscape() {
return isDevicePortrait() && (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}
and the isDevicePortrait() would be like:
private boolean isDevicePortrait() {
return (findViewById(R.id.A_View_Only_In_Portrait) != null);
}
*Notice that we cannot use getResources().getConfiguration().orientation to determine if the device is currently literally Portrait. It is because the Resources object is changed RIGHT AFTER the screen rotates - EVEN BEFORE onSaveInstanceState() is called!!
If we do not want to use findViewById() to test orientation (for any reasons, and it's not so neat afterall), keep a global variable private int current_orientation; and initialise it by current_orientation = getResources().getConfiguration().orientation; in onCreate(). This seems neater. But we should be aware not to change it anywhere during the Activity lifecycle.
*Be sure we remove_fragments() before super.onSaveInstanceState().
(Because in my case, i remove the Fragments from the Layout, and from the Activity. If it is after super.onSaveInstanceState(), the Layout will already be saved into the Bundle. Then the Fragments will also be re-created after the Activity re-creates. ###)
### I have proved this phenomenon. But the reason of What to determine a Fragment restore upon Activity re-create? is just by my guess. If you have any ideas about it, please answer my another question.
In your onCreate() method you should only create your ListFrag if you are in portrait mode. You can do that by checking if the FrameLayout view that you only have in the portrait layout is not null.
if (findViewById(R.id.yourFrameLayout) != null) {
// you are in portrait mode
ListFrag listFrag = new ListFrag();
getSupportFragmentManager().beginTransaction()
.replace(R.id.FragmentContainer, listFrag).commit();
} else {
// you are in landscape mode
// get your Fragment from the xml
}
In the end if you switch from portrait to landscape layout, you don't want to have another ListFrag created, but use the Fragment you have specified in your xml.
#midnite ,first of all thanks for your question, I had the same question, on wich I worked for days. Now I wound some tricky resolution to this problem - and want to share this to community.
But If someone has better solution, please write in comments.
So, I have same situation - different layouts for two device orientation. In portrait there is no need for DetailsFragment.
I simply in OnCreate trying to find already created fragments:
fragment = (ToDoListFragment) getFragmentManager().findFragmentByTag(LISTFRAGMENT);
frameDetailsFragment = (FrameLayout) findViewById(R.id.detailsFragment);
detailsFragment = (DetailsFragment) getFragmentManager().findFragmentByTag(DETAILS_FRAGMENT);
settingsFragment = (SettingsFragment) getFragmentManager().findFragmentByTag(SETTINGS_FRAGMENT);
Right after that I am adding my main fragment -
if (fragment == null){
fragment = new ToDoListFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, fragment, LISTFRAGMENT)
.commit();
}
And clear back stask of my fragments:
if (getFragmentManager().getBackStackEntryCount() > 0 ) {
getFragmentManager().popBackStackImmediate();
}
And the MOST interesting is here -
destroyTmpFragments();
Here is the method itself:
private void destroyTmpFragments(){
if (detailsFragment != null && !detailsFragment.isVisible()) {
Log.d("ANT", "detailsFragment != null, Destroying");
getFragmentManager().beginTransaction()
.remove(detailsFragment)
.commit();
detailsFragment = null;
}
if (settingsFragment != null && !settingsFragment.isVisible()) {
Log.d("ANT", "settingsFragment != null, Destroying");
getFragmentManager().beginTransaction()
.remove(settingsFragment)
.commit();
settingsFragment = null;
}
}
As you can see, I clean manually all fragments, that FragmentManager gentfully saved for me (great thanks to him).
In log I have next lifecycle calls:
04-24 23:03:27.164 3204 3204 D ANT MainActivity onCreate()
04-24 23:03:27.184 3204 3204 I ANT DetailsFragment :: onCreateView
04-24 23:03:27.204 3204 3204 I ANT DetailsFragment :: onActivityCreated
04-24 23:03:27.204 3204 3204 I ANT DetailsFragment :: onDestroy
04-24 23:03:27.208 3204 3204 I ANT DetailsFragment :: onDetach
So in onActivityCreated make your views check for null - in case fragment is not visible. It will be deleted little bit later (It is so maybe cuz fragment manager's remove is async)
After, in activity's code we can create brand new Fragment instance, with proper layout (fargmnet's placeholder) and fragment will be able to find it's views for example, and will not produce NullPointerException
But, fragments that are in back stack are deleted pretty fast, without call to onActivityCreated (with th help of above code:
if (getFragmentManager().getBackStackEntryCount() > 0 ) {
getFragmentManager().popBackStackImmediate();
}
At last, at the end i add fragment if I am in land orientation -
if (frameDetailsFragment != null){
Log.i("ANT", "frameDetailsFragment != null");
if (EntryPool.getPool().getEntries().size() > 0) {
if (detailsFragment == null) {
detailsFragment = DetailsFragment.newInstance(EntryPool.getPool().getEntries().get(0), 0);
}
getFragmentManager().beginTransaction()
.replace(R.id.detailsFragment, detailsFragment, DETAILS_FRAGMENT)
.commit();
}
}

Restoring Fragment state after configuration change if Activity layout changes

My Activity has a one-pane layout in portrait and a two-pane layout in landscape, using Fragments. I'd like the system to restore all the views after an orientation change, but I don't know where to start.
Is this even possible considering the layout is different (but similar) in the two orientations?
If anyone has implemented this (or at least tried), please share you experience.
Maybe I know what the problem is. Hope my answer helps someone want to solve such problem.
In case there are a Fragment in your Activity, you config that Activity in AndroidManifest.xml with
android:configChanges="keyboardHidden|orientation|screenSize"
then, you want to change the layout of the Activity when onConfigurationChanged triggered. Once the Activity inflate a new layout or using setContentView(R.layout.new_layout);, the fragment will disappear, however, that fragment still running and just disappear from the view.
So the problem is that fragment is attached to the previous layout, what you need to do:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (fm.findFragmentById(R.id.pane_list) == null) {
// instantiate fragment and add to view
mFragment = new ItemFragment();
transaction.add(R.id.pane_list, mFragment );
} else {
// fragment already exists, we re-attahced it
mFragment = fm.findFragmentById(R.id.pane_list);
transaction.detach(mFragment);
transaction.attach(mFragment);
}
transaction.commit();
After detached from old layout and attached to new layout, problem solved.
Correct me if there any wrong :)
I urge you to use a Fragment with setRetainInstance(true) in its onCreate method. This makes the fragment retain its member variables across Activity configuration changes. Please read this blog post which explains the information you need:
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
Which you can use later like this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
}

Categories

Resources