1

I'm trying to use Fragments in android.

So in the method "getItem()" into the class "TabAdapter", I have:

public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Browse tab1 = new Browse();
                return tab1;
            case 1:
                Create tab2 = new Create();
                return tab2;
            default:
                return null;
        }
    }

So, in class "Browse", I use inflate to put into TabLayout fragment, the layout that I want to use:

public class Browse extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_browse, container, false);
        return rootView;
    }

And in this class, a inner class that has the method "onCreate()"

   public class BrowseInner extends AppCompatActivity{

        DBManager db = new DBManager(this);
        DBManager.DatabaseHelper dbh = new DBManager.DatabaseHelper(this);

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.out.println("TRYTOSTAMP");
            setContentView(R.layout.activity_browse);
            Cursor cursor = dbh.giveAllItemFromDB();
... //"and all the logics to interact with layout"...
}

But, in this way, I have only the layout in the Fragment TabLayout, as it is defined in .xml design, but I can't interact whit it (query not processed, so fields in the layout not fill, an event on button not called and so on...) Where am I doing wrong? Thanks in advance.

kelalaka
  • 5,064
  • 5
  • 27
  • 44

1 Answers1

0

If I am correct you want to put the data that you get in BrowseInner into the fields of the two Fragments?

In general you are doing everything wrong.

You have no meaningful names like BrowseInnerActivity, BrowseFragment and CreateFragment

You can return like this. No need to assign.

public Fragment getItem(int position) {
        switch (position) {
             case 0:
                    return new Browse();
                case 1:
                    return new Create();
                default:
                    return null;
        }
    }

Why are you trying to inflate the same layout both in the Activity and the Fragment? Even if the views should be the same you need to create separate layouts and name them accordingly. You can not inflate in Fragment: R.layout.activity_browse

And the above is basic programming. Your design is not correct. The Activities and Fragments are "Views" in terms of MVP, MVVM and etc. They need to be passive. Only present some data and notify something, for example the ViewModel about some actions executed on them.

So in your case the Activity can only create the Fragments. You can create a ViewModel, which is shared between the two Fragments. The ViewModel can hold a reference to some Repository and update LiveData objects in itself. Then Fragments can observe the LiveData and do with the data what they need to do. There are pretty good design guidelines:

https://developer.android.com/jetpack/docs/guide

And here are some sample apps:

https://github.com/android/architecture-samples

And in general you should understand what is MVVM,ViewModel, LiveData and etc.

Yavor Mitev
  • 1,363
  • 1
  • 10
  • 22