Reputation: 155
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?
Upvotes: 0
Views: 46
Reputation: 350
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.
Upvotes: 0
Reputation: 25830
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.
Upvotes: 1