Application has two fragments: the first one contains a small representation of pager with photos, and the second one contains full screen pager. The second fragment replaces the first and passes a page number to the previous every time it changes. I made connection between my fragments just like Android Developers says.
Everything works till device orientation doesn't change. The first fragment is not recreated until it is not on top of stack, that is why all page number changes after that are missed for first fragment.
I do not really what to disable views destroy on orientation change, but looks like it is the only way.
What is the best solution?
In your manifest file in your parent activity in which fragments are write following line :
android:configChanges="keyboardHidden|screenSize|orientation"
Let me know if that works for you. Best of luck :)
When you change the orientation, the activity gets rebuild, thus essentially destroying the fragment that was built before the change in orientation.
Perhaps you would wish to refer to this https://developer.android.com/guide/topics/resources/runtime-changes.html on retaining the state during a change in configuration.
Related
I am making a weather app. In MainActivty (extends AppCompatActivity) it shows the current weather. On this screen there are two buttons: 1 button that opens the SettingsActivity (new screen) and 1 button that opens ListviewActivity(new screen which shows short weatherconditions of all capitals in Europe in a listview)
I haven't used any fragments, but I'm not sure if this is right... I thought fragments aren't needed, because every Activity just has 1 screen. But I read on internet it is good practise to always use fragments, even when an activity only has 1 screen.
Also, is it easy to convert my screen in MainActivity to a fragment?
Could you please give an example of this?
I haven't used any fragments, but I'm not sure if this is right... I
thought fragments aren't needed, because every Activity just has 1
screen. But I read on internet it is good practise to always use
fragments, even when an activity only has 1 screen.
Yes, Its always a good practice to use Fragments event though as of now you are having 1 screen for each of your activity. The main reason is directly related to future phases or features your application may have, so that it would be easier to make your application scalable and also to make your activity clean and do complex individual things in Fragments.
Also, is it easy to convert my screen in MainActivity to a fragment?
For this, you need to make certain changes for both Activity and Fragments. But its upto you(particularly in your current app) how you are presuming the future updates. I would strongly recommend you to use Fragments even in current situation of the app.
Hope it helps.
Yes, the fragments make the app to load much faster as the load on the main thread will be reduced and you can even apply animations to the fragments when it opens, that makes it look even cool and attracts attention.
Coming to your question, the answer is "yes" you can easily convert your MainActivity into a fragment if you haven't done much work in the MainActivity.
Firstly, know what are fragments and how to use them by clicking here.
After knowing what fragments are, all you need to do is create a blank fragment and do all the work that you are doing in MainActivity inside a fragment that you've created and create a newInstance of this fragment in the onCreate method of your MainActivity and replace the MainActivity's layout with fragment by using the FragmentManager's replace function.
Hope, this helps you.
I have two screens and two xml-files for one activity. In my onCreate method I call the fist one and handle some user input. After that I am changing my layout with
setContentView(R.layout.activity_quiz2);
and everything is fine and works as intended but when I run the application and turn my device after I switched the layouts. It will just switch back to the first layout and stay on that layout (every button and all works as well).
How can I prevent my screen from changing back my layouts when I turn my device?
It's switching back to the first layout because when you rotate the screen the whole Activity is recreated. You have couple of options to deal with this:
Add android:configChanges="orientation" to your <activity element in the AndroidManifest.xml file. This way you're telling the system you want to deal with the rotation yourself (and if needed you can perform some actions in onConfigurationChanged() method).
Use Fragments to display the two "states" you have (I'm assuming those are quiz1 and quiz2). This way you won't need to do multiple setContentView() in your activity.
The second option is the one to go for, as that's basically what Fragments are all about. Not to mention you can do very nice transitions between them (fade-in-out animations, etc) and your UX will be much better than when swapping the setContentView().
I use framgents in my app and I have a dual pane layout for both landscape (left and right) and portrait (up and down) on a tablet.
I have an activity which loads a fragment into the left selection pane and then you can choose several criteria in some Spinner to do a search. In right fragment is being shown results from search in a ListView.
If I use android:configChanges="orientation" in this activity in Manifest.xml the results from search are saved but I have both pane layouts up and down in landscape mode instead of left and right, as it should be.
And if I do not use android:configChanges="orientation" I have both pane layouts in order correct (left and right in lanscape mode) but I do not have results from search, activity restarts.
How I can solve it? Any idea?
Thanks so much.
You should be using onSaveInstanceState() and onRestoreInstanceState()
You need your view to be recreated for the layout to look correct but on recreation the data will be lost. You must save this data using the methods I mentioned or by using the shared preferences (for saving data longer that lasts after application exit).
Do not use android:configChanges="orientation". This prevents the layout from being recreated and expects you to deal with it yourself.
See the documentation for more info:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
http://developer.android.com/reference/android/content/SharedPreferences.html
I am developing an app for android lately. I decided to use power of fragments and decided to use dynamic UI. I want to have one fragment included in portrait orientation and two in landscape.
I figured out how to create two layouts and how to add fragments.
Everything is going pretty well until now.
I am trying to find a proper approach to creating and destroying fragments on rotation. I know that I should always check if there is a saved instance, so on rotation there should not be more fragments created. But this solution ignores creating another fragment when user switch from portrait to landscape. So I thought that I need to check orientation before checking if instance was saved. Checking is not the problem but what is the proper way to work with fragments? Should I always create a new fragment and on change rotation remove it? Or have it hidden?
I found one implementation where is activity that have both fragments and when the rotation is changed the new activity was created. But this solution create activity and fragment for one thing and I am not sure if this is proper approach.
What do you suggest? I think fragments are badly covered by official site for android developers.
Maybe this tutorial by Lars Vogel can help you:
Tutorial
He creates a landscape application and shows in chapter 11, how to change to portrait mode.
What I expected in my application is data coming from the server and everything, even the view, are dynamically created. The activity layout is divided into two parts. On the left is a list and on the right side are dynamic fragment changes. Whenever the orientation changes the server call is made again (As the activity is destroyed and re-created). This is working fine.
There are 3 fragments(2 are Maps) that have to be called from inside of each other on the right side of the layout.
Now I dont want this thing to happen. I don't want everything to refresh again and start from scratch.
I can't set configChange in the manifest, as the image sizes in the layout for landscape and portrait are somewhat different.
What is needed is a layout change on orientation change without re-creating the activity.
I have tried using onSaveInstanceState() and onRetainNonConfigurationInstance() but couldn't get it to work.
I think your solution is to set the configChange on your manifest AND create different layouts for portrait and landscape so you can have the results you are expecting: No refresh of the activity, and control the sizes of your ImageViews.