One fragment to another fragment in android

how to send data from one fragment to another fragment in android?

Hello I make a simple tab view using pager and fragment . So I have two tabs in my view .In one tab I have list view in which each row have textview and favourite image button .In second Tabs I need to show all item name which is favourite in first tab .so I need to send a list from one fragment to another another list .

here is my code

Mainactivity.java

activity_main.xml

fragmentone.java

customAdapter.java

datamodel.java

fragmentone.xml

rowlayout.xml

fragmenttwo.java

fragmenttwo.xml

As shown in image above I select first station is my favourite station .I need to display on second tab ? can it is possible ?

3 Answers 3

Suppose you are sending data from FragmentA to FragmentB, then the best practice is to use Interfaces and then communicate between fragment via the container activity. Below is a small snippet that will provide with the skeleton of what i am trying to say:

Step-1: In your FragmentA define an Interface and override onAttach() method to make it mandatory for your container activity to implement the interface and provide body to its method.

Now, in your FragmentA you can pass value to myMethod() like: if (listener!=null)

Step-2: Now, In your container activity implement the callback Interface

Step-3: In FragmentB have a method say for example methodInFragmentB(yourParameters)

I hope the above description helps. Thanks.

Источник

Fragment over another fragment issue

When I’m showing one fragment (which is full screen with #77000000 background) over another fragment (let’s call it main), my main fragment still reacts to clicks (we can click a button even if we don’t see it).

Question: how to prevent clicks on first (main) fragment?

EDIT

Unfortunately, I can’t just hide main fragment, because I’m using transparent background on second fragment (so, user can see what located behind).

12 Answers 12

Set clickable property on the second fragment’s view to true. The view will catch the event so that it will not be passed to the main fragment. So if the second fragment’s view is a layout, this would be the code:

Solution is pretty simple. In our second fragment (that overlaps our main fragment) we just need to catch onTouch event:

Just add clickable=»true» and focusable=»true» to parent layout

If you are using AndroidX , try this

You should hide the first fragment when you are showing the second Fragment if two fragments is placed in same container view.

If you want to know more questions about how to solve problems about Fragment, you can see my library: https://github.com/JustKiddingBaby/FragmentRigger

You need to add android:focusable=»true» with android:clickable=»true»

Читайте также:  Top android мой том

Clickable means that it can be clicked by a pointer device or be tapped by a touch device.

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

Metod 1:

You can add to all fragments layout

Metod 2: (Programmatically)

Extend all fragment from FragmentBase etc. Then add this code to FragmentBase

There is more than one solution that some of us contributed to this thread but also I would like to mention one other solution. If you don’t fancy putting clickable and focusable equals true to every layout’s root ViewGroup in XML like me. You can also put it to your base if you have one just like below;

You can also use inline variable but I did not prefer it for personal reasons.

I hope it helps for the ones who hate layout XMLs.

The acceptable answer will «work», but will also cause performance cost (overdraw, re-measuring on orientation change) as the fragment on the bottom is still being drawn. Maybe you should simply find the fragment by tag or ID and set visibility to GONE or VISIBLE when you need to show again.

Источник

Replace one fragment with an another fragment

I want to replace an old Fragment with a new Fragment , but i still get the buttons of the old Fragment that is still visible in the new fragment.

In the old one, on a button click

I can replace the old Fragment with the new one, but the buttons from R.id.allmoods Fragment still visible on top of the new Fragment .

I tried with this given below code.

This is the fragment that is supposed to replace the above:

Both doesn’t work. What to do? UPDATE: After replacing with the proper container the buttons had gone but the new fragment is not getting instantiated properly. I gets a pure blank white screen.

8 Answers 8

I Have worked on fragments before and hope this would help you out and give you a better understanding of the flow. Firstly, your MainActivity.xml file will look like this :

Next, you create two fragments and their XML is mentioned below : fragment1.xml

The next fragment would look exactly the same as mentioned above. Here is the Fragment1.class :

And the Fragment2 would be as follows :

As I mentioned earlier, the xml file would be the same as fragment1.xml . What is more important here is that the main activity will contain a layout which will take the fragments view when ever the user switches fragments. Hence we use the replace method which would just replace the previous view with the fragments view specified by us.

Читайте также:  Как запустить андроид сдк

To understand the flow of fragment transition, first of all, you have to know about its structure in activity. Let’s see: a) Activity: At bottom of everything (MainActivity)

Here @+id/container is the layout over we do transitions of fragment contents.

B) FragmentA : Initially added fragment to the container of MainActivity.

B) FragmentB : Replace FragmentA with FragmentB

So main thing behind this is to replace/add fragment content view to the activity container view.

In your Activity’s onCreate function, you should be calling setContentView(R.layout.main) , then when you want to load a Fragment, you choose a ViewParent within R.layout.main . The fragment will become the child of that ViewParent. So the id passed into FragmentTransaction.replace, is the id of the ViewParent in R.layout.main .

It makes sense that the Button in your allmoods RelativeLayout would remain because the FragmentTransaction.replace function only replaces an existing fragment that is in that container. Everything in R.layout.main will remain. This is how an Activity keeps static content, like drawers or toolbars.

When you load your «new fragment» you will use the same id. So the «new fragment» replaces the «old fragment» as the new child of the ViewParent within R.layout.main .

Update:

When you call FragmentTransaction.replace in your Activity’s onCreate function, this could be recreating an existing Fragment. Make sure the savedInstanceState (the Bundle passed into onCreate) is null. If the savedInstanceState is not null, then the fragment probably already exists and you can find it like this;

Update 2:

Here is a guide that should help you. It looks like you could use FragmentPagerAdapter to simplify your fragment transactions.

Источник

Android set clickable text to go one fragment to another fragment

I need to do something like this. Suppose I have 2 fragments A and B.There is a text which can be clickable in fragment A and when user click this text , he can go to fragment B. This example helped me to do it but I think it does not work for fragment. So please tell me a way to solve this problem.

4 Answers 4

If LoginActivity is a fragment class then it would be okay if you use setOnClickListener on textview. But for fragment change you have to change Intent to fragmentTransaction,

Use something like,

But, if you want to use SpannableString then do like this,

Here, R.id.container is the fragment of your main activity layout in which new view will be replaced.

You can not call Fragment via Intent . You need to replace your current fragment with new one.

Читайте также:  Лучший шутер для андроид 2021

you have to replace your fragment A to B, use this code

In This code replace R.id.youframelayoutid, then it workable

If its code useful so please mark me my answer. 🙂

As an usual manner you should put a FrameLayout in your Activity’s layout XML file. This FrameLayout acts like a placeholder for your Fragments. In other words, Fragment A can be pasted there, so is for Fragment B.

Okay, suppose you’ve added a FrameLayout in you activity’s layout file. Pasting fragments on it and also replacing fragments should be done by the FragmentManager . Hence, you should grab a reference to a FragmentManger in your activity class. For getting this done .

If you use Android Support Libraries, you should get a reference to FragmentManger by getSupportFragmentManager()

In Android adding fragments and also replacing them are done in the form of a transaction. Thus you should inform the fragment manager that you would like to do a transaction. This could be done via:

Now, you can apply all what you want on this transaction object. For instance, Adding a fragment could be done like this:

After you’re done, you commit that transaction by calling

Notes:

FragmentManager acts like a container for your added fragments. You can search through your added fragments by their tag.

Device rotation does not remove added fragments in the FragmentManager. Thus in your onCreate method take care you’ve added any fragments only once.

You can add a Transaction to the back stack. This means that whenever user clicks on the Android back button this fragment will be removed from the state stack and also will be rolled back.

Источник

How to pass Bundle from Fragment to Fragment

I am using fragments in my application. This is my first fragment that simply inflate a xml file:

and this is the left_panel .xml, that contains a fragment:

This is my ListFrag class:

I don’t know how to pass Bundle arguments from FragmentA to ListFrag.

4 Answers 4

In your FragmentA fragment set the bundle as the argument.

In your ListFrag fragment get the bundle as

Fragment to Fragment set and get Argument:

this work for me,try this may be this sample help you.

Create a static method of ListFrag in ListFrag such as:

When you create a ListFrag from Fragment A, you would call:

You have two choice :

A. If i you have reference of your ListFragment You can set target fragment for FragmentA by doing this :

  1. in FragmentA this.setTargetFragment(yourListFragment);
  2. then this.getTargetFragment().setArguments(yourBundle);
  3. and in ListFragment get it back with in with this.getArguments();

B. The most logic way

I bet your fragment are in the same activity so she have references of them. You can pass data to your activity from FragmentA and pass it to ListFragment

FragmentA —Data—> FragmentActivity —Data—> ListFragment

Источник

Оцените статью