0

I have a activity with only 2 buttons and a FrameLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout">
    <Button
        android:id="@+id/btPrincipal1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ver Fragment 1"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/btPrincipal2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ver Fragment 2"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"/>

</LinearLayout>

<FrameLayout
    android:layout_width="250dp"
    android:layout_height="match_parent"

    android:id="@+id/contenedor"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/linearLayout">


</FrameLayout>

When I click button 1, I call the fragment and, when I click button 2, I want to call the same fragment but with different properties. For example:

I click button 1 , call fragment A with background color green.

I click button 2 , call fragment A with background color red.

It's possible? and how can I do this? Thanks.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
Luis Yul
  • 45
  • 1
  • 4
  • See the answers from below links: http://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back http://stackoverflow.com/questions/28829509/how-to-pass-arguments-to-fragment-from-activity – Asif Patel Apr 04 '17 at 20:50
  • Possible duplicate of [How to pass a variable from Activity to Fragment, and pass it back?](http://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back) – Gary99 Apr 05 '17 at 01:04

2 Answers2

0

If you want to change the background just create and object of your fragment, and after clicking on buttons call a method to change the fragment background. but if you wanna to change fragments layout or other things create a new fragment cause of code readability.

try this:

public class FragmentA extends Fragment {
    public static FragmentA newInstance(int colorResId) {
        FragmentA fragment = new FragmentA();
        Bundle bundle = new Bundle();
        bundle.putInt("colorId", colorResId);
        fragment.setArguments(bundle);
        return new FragmentA();
    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resId = getArguments().getInt("colorId", 0);
        if (resId != 0) {
            //set background
        }
    }

    public void changeBackground(int colorId){
        //set background
    }
}
FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
0

In your MainActivity, do:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1 = (Button) findViewById(R.id.btPrincipal1);
    Button button2 = (Button) findViewById(R.id.btPrincipal2);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            Bundle bundle = new Bundle();
            bundle.putString(COLOR_PARAM, "green");
            supportFragmentManager.beginTransaction()
                    .replace(R.id.contenedor, YourFragment.newInstance("Button1"))
                    .commit();
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            Bundle bundle = new Bundle();
            bundle.putString(COLOR_PARAM, "red");
            supportFragmentManager.beginTransaction()
                    .replace(R.id.contenedor,  YourFragment.newInstance("Button2"))
                    .commit();
        }
    });
}

Then, in YourFragment.java, do:

public class YourFragment extends Fragment {

public static final String COLOR_PARAM = "param1";

private String currentColor;

public YourFragment() {}


public static Fragment newInstance(String param) {
    Fragment fragment = new Fragment();
    Bundle args = new Bundle();
    args.putString(COLOR_PARAM, param);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        currentColor = getArguments().getString(COLOR_PARAM);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
    FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_layout);
    if (currentColor.equals("green")) {
        frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_green_dark));
    }
    else if (currentColor.equals("red")) {
        frameLayout.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_red_dark));
    }

    return view;
}
}
Roger
  • 76
  • 1
  • 4
  • Thanks for u answer!! but this now work, in MainActivity, COLOR_PARAM is a String? – Luis Yul Apr 05 '17 at 07:19
  • In MainActivity, COLOR_PARAM is the same as the Fragment. If they were not in the same package you would have to do YourFragment.COLOR_PARAM – Roger Apr 05 '17 at 13:56