-1

First of all, I'm new in Android.

I tried to use Fragments with Bottom-Navigation and my question is, every time I change my Fragment (for example, fragment1 -> fragment2) my tabs doesn't change with the change in Fragment and everything I did in fragment1 is not saved.

To make it clearer:

I got bottom navigation bar/tab (I'm not sure what to call it). In that bar I have got 3 tabs (For example; chats, profile and contacts).

  1. First I go to the profile tab and change something (for example my name). Then I go to the contacts and when I come back to the profile tab, the name that I changed earlier isn't there. It goes back to the information previously saved.

How can I save this information? Do I have to write all the information in the database and read it every time I open the profile fragment? Is it even possible?

Or can I save it in the SharedPreference or somewhere else? Please give me some advice with example code so that I can understand..

  1. For example, I have a button in profile fragment and with this button it will change to chat fragment.

    FragmentManager().beginTransaction().replace(R.id.frame,fragmentTwo).commit();

When I do this, my fragment changes to another fragment, but my tabs in navigation bar doesn't change and it remains in the profile tab.

  1. Last Question. If I change my fragment what kind of function it does run? I mean there are onCreate, onCreateView, onActivityCreated etc. and how can I understand how every function runs. After each other with that sequence.

Please help. I'm stuck for weeks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
W.Yen
  • 1
  • 4

1 Answers1

0
  1. You can simply use the ViewPager along with a TabLayout. To get a bottom navigation bar using TabLayout please see the answer here. The information that you edited in the profile fragment should stay in the profile fragment as you're not replacing the current fragment with another when you're using the TabLayout.

  2. While implementing TabLayout using ViewPager you do not have to be worried about making transactions among your Fragments anymore as they are already populated when the Activity was loaded. You'll be handling the fragment transactions via FragmentPagerAdapter.

  3. To understand the lifecycle of a Fragment you need to check the docs in the developer.android.com. See the Creating a Fragment section for better understanding in which sequence the functions are being called.

Implementing TabLayout in your application is fairly simple. Please check the documentation here for better understanding. Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98