onStop for Fragment in TabLayout to save some content - java

I have a TabLayout with two Tabs or Fragments.
In one Fragment I want to save for example text from edittext before switching tab or closing app.
I tried with onStop. It save the content before closing app. But when I go to second tab, the app closes without exception.
Why? Are the a solution for this problem?

Related

Fragment showing nothing when starting my app for first time

I have an app with bottom navigation fragment activity
I have placed text views and buttons on my home fragment.
further these fragments are placed in a frame layout.
when I launch my app my home fragment shows nothing just blank screen it loads everything when I
switch between fragments.
I want to fix this, so that when I launch my app everything that is on my home fragment shows up.
because you didn't specify the first fragment to show,
in the onCreate you have to do this :
first checked the bottomNavigation item, that you want to start with :
bottomNavigation.getMenu().findItem(R.id.item).setChecked(true);
then put the first fragment in frame layout

What is the best way to create pages(about/my home/references tabs) as in the image below?

I just started learning Android programming. I've created a page and I want to create "About/My Home/References" tabs in the image below.
The "About/My Home/References" tabs (you can see them in the link below) all act like a button. When I click the "About" tab, I can see the things about the users. When I click the "My Home" tab, I can see the some text about my home. The third tab also does the same thing.
Image link: https://imgyukle.com/i/4LgQH
My question is how can I do this in Android Studio? I've added three button but it doesn't look like that. Any idea?
You can use TabLayout with TabItems widgets. Visit, for example, https://material.io/develop/android/components/tab-layout/ for short overview and https://www.codingdemos.com/android-tablayout-example-viewpager/ as step-by-step manual.
For that layout, you want to use Fragments and a ViewPager with a TabLayout.
Your main Activity would contain 3 Fragments, AboutFragment, HomeFragment and ReferencesFragment.
This would allow you to build each layout separately. The ViewPager would contain the Fragment, and lets you swipe between them. The TabLayout contains your buttons, to tell the ViewPager which to show.

How to add / remove tabs from inside a fragment (AppCompat)

I am having a really tough time with one small issue in my AppCompat activity with tabs. I am using the Android Design Support Library, and have implemented a tabbed application with fragments.
Now, I have no problem creating tabs & fragments in the Activity's onCreate() method, but I cannot for the life of me find if it is possible to add tabs programmatically from within a fragment.
For reference, all of my tabs use the same fragment (OneFragment.java). I have tried using FragmentManager / FragmentTransaction, but although this creates a fragment (I think!), it does not make a tab.
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI. Any help would be much appreciated!
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI
If you managed to add a tab to the FragmentPagerAdapter but the UI didn't change then you probably forgot to call the notifyDataSetChanged() method on it.

Tablayout , disable change between tabs with slide

I made a TabLayout and it's work fine , but I want to disable change between tabs with slide and make it change tabs only by clicking the tab name
is there a way to do it?
Just use the TabLayout without the ViewPager. In your onTabSelected, you load the view or fragment you want into the page container.

Putting ViewPager and tabs in a fragment and replacing it

In my main activity I have a ViewPager with two tabs and each of them has its contents in a fragment. One of the tabs holds a list of items and when I press on an item from that list I want the details of that item to appear on the screen. I thought of putting the whole ViewPager in a fragment and when the user taps the item the fragment representing the whole ViewPager is replaced with another fragment representing the details of the item. Is it possible and is it a good practice? Maybe I should just start another activity with the details of the item?
How you do it is your own choice. It's perfectly fine to switch your Fragments.
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FragDetails())
.addToBackStack(FragPager.class.getName()).commit();
Switching to another Activity is fine too, you just have more of a "flicking" screen when opening a new Activity (and some other life cycle stuff too of course). If you don't want that, switch Fragments.

Categories

Resources