Question

First time posting here and also a new developer for Android Apps.

Desire Function of Example App:

1) Activity starts in fullscreen A

2) Fragment B then populates/inflates container of A fullscreen

3) Fragment B has a button, button is press

4) Fragment B is now replaced with Fragment C

5) Fragment C is now full screen and has data that is inputted by users, user then hits button to send to next Fragment

6) Fragment C is replaced with Fragment D and presents data to view which was inputted from Fragment C

Summary Functionality:

I am trying to keep everything on one screen going from

A (Activity) -> B (Fragment replace) -> C (Fragment replace to type data) -> D (Fragment replace and see last fragment data)

Problem/Issue

My code crashes when I try to populate the last screen with data obviously. It seems to throw an error in MainActivity at this specific line when I debug it

CartFragmentCode addCartInfoTextFragment = (CartFragmentCode) getSupportFragmentManager().findFragmentById(R.id.maincontainer);

I am definitely passing the values correctly through the interface I created, but during the debug process, I found out that my current program is trying to populate information into the same container since it seems like it didn't commit yet to replace the fragment to assign the variables to the right UI.

I was checking if I was able to replace the Fragment C with D, and I was able to only if I remove that above line of code.

Code Below

Main

public class MainActivity extends FragmentActivity implements OrderFragmentCode.FragUIListeners{

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

    Fragment AllMenu = new AllMenuFragmentCode();

    FragmentManager fragManager = getSupportFragmentManager(); //getSupportFragmentManager setup
    FragmentTransaction ft = fragManager.beginTransaction(); //setup fragmentTranscation = ft

    ft.add(R.id.maincontainer, AllMenu);
    ft.commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onCartButtonClicked(String editFood, String editType, String editName){

    CartFragmentCode addCartInfoTextFragment = (CartFragmentCode) getSupportFragmentManager().findFragmentById(R.id.maincontainer); 
    addCartInfoTextFragment.UpdateCartTexts(editFood, editType, editName);
}}

Fragment A

 public class AllMenuFragmentCode extends Fragment implements OnClickListener {
private Button Order;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.all_menu_fragment, container, false);
    Order = (Button) view.findViewById(R.id.orderButton);
    Order.setOnClickListener(this);
    return view;
}
@Override
public void onClick(View v) {   
    Fragment OrderFragmentCode = new OrderFragmentCode();
    FragmentManager fragManager = getFragmentManager(); //getSupportFragmentManager setup
    FragmentTransaction ft = fragManager.beginTransaction(); //setup fragmentTranscation = ft   
    ft.replace(R.id.maincontainer, OrderFragmentCode);
    ft.addToBackStack(null);
    ft.commit();    
}}

Fragment C

public class OrderFragmentCode extends Fragment implements OnClickListener {

FragUIListeners activityCallback;

public interface FragUIListeners {
    public void onCartButtonClicked(String foodText, String typeText, String nameText);
}
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCallback = (FragUIListeners) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement FragUIListeners");
    }
}
private EditText editTextFood;
private EditText editTextType;
private EditText editTextName;
private Button AddToCartButton;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.order_fragment, container, false);
    editTextFood = (EditText) view.findViewById(R.id.editText);
    editTextType = (EditText) view.findViewById(R.id.editText2);
    editTextName = (EditText) view.findViewById(R.id.editText3);
    AddToCartButton = (Button) view.findViewById(R.id.addToCart);
    AddToCartButton.setOnClickListener(this);
    return view;
}   
@Override
public void onClick(View v) {
    Fragment Cart = new CartFragmentCode();
    FragmentManager fragManager = getFragmentManager();
    FragmentTransaction ft = fragManager.beginTransaction();
    ft.replace(R.id.maincontainer, Cart);
    ft.addToBackStack(null);
    ft.commit();
    activityCallback.onCartButtonClicked(editTextFood.getText().toString(), editTextType.getText().toString(), editTextName.getText().toString());  
}}

Fragment D

public class CartFragmentCode extends Fragment{
private TextView foodView;
private TextView typeView;
private TextView nameView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.cart_fragment, container, false);
    foodView = (TextView) view.findViewById(R.id.foodView);
    typeView = (TextView) view.findViewById(R.id.typeView);
    nameView = (TextView) view.findViewById(R.id.nameView);
    return view;
}
public void UpdateCartTexts(String editfood, String edittype, String editname)
{
    foodView.setText(editfood);
    typeView.setText(edittype);
    nameView.setText(editname);
}}     

--Final Say-- Sorry for the long post, this has been bugging me for HOURS and I even tried to use bundles and setting the arguements but I wasnt able to get that working either (I thought if i were able to obtain values correctly I could work around and use this to assign values to my textviews through getArguments on my last Fragment).

Please help!!!! Thanks!!!

Was it helpful?

Solution 3

This is was one of my resource at the time to mimic a solution (he was using two fragments in one activity, while my app is trying to use one activity and replacing the main framelayout): http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example

After playing around with example codes and using the available documentations. I cannot use these two lines to transfer the information as suggested in my MainActivity:

CartFragmentCode addCartInfoTextFragment = (CartFragmentCode) getSupportFragmentManager().findFragmentById(R.id.maincontainer); 
addCartInfoTextFragment.UpdateCartTexts(editFood, editType, editName);

I think I finally found a workaround or a solution? I am not sure if this is the correct way of passing information to fragments or other classes, but here it is...

I basically Took away those two lines and rework my MainActivity's Click method and changed it to this...

public void onCartButtonClicked(String editFood, String editType, String editName){
    CartFragmentCode.passFood = editFood;
    CartFragmentCode.passType = editType;
    CartFragmentCode.passName = editName;
}

I then went to my last fragment Code which was OrderFragmentCode.java and declared my Strings to retrieve them at the top of the Class and they were...

 public static String passFood;
 public static String passType;
 public static String passName;

I then took the those values and setText them to my textViews. Wallah! It works as intented, however I am not sure if this is the right way to do things... If you guys are able to find a more professional way to handle data across multiple fragments, please let me know!

OTHER TIPS

In the line CartFragmentCode addCartInfoTextFragment = (CartFragmentCode) getSupportFragmentManager().findFragmentById(R.id.maincontainer); findFragmentById sholule be passed that you use as 2nd argument in add while adding fragment to container for e.g. ft.add(R.id.maincontainer, AllMenu); if you want to get this fragment then you use findFragmentById(AllMenu) which is the tag that is associated with the fragment. R.id.maincontainer is the container in which that fragment is being displayed.

I guess the problem is you call onCartButtonClicked inside fragment C (where fragment C still exist) so the fragment D isn't created and take C place yet.

Try to call onCartButtonClicked by a Handler in your MainActivity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top