0

Hi I am new to android if someone is able to help me to fix this issue it will be much appreciated. Now I am trying to go back to the previous view or the root view do anyone know how to go back to the previous view from static context(I am not sure static context is the correct way to say it do feel free to correct me if I am wrong)?

I have already tried creating FragmentManager the reason I use this is because the application I am trying to create used Fragment.

 FragmentManager fragmentManager = 
 ((FragmentActivity)mContext).getSupportFragmentManager();
            fragmentManager.popBackStack();

`

public class SomeView1 extends View implements View.OnTouchListener {


public SomeView1(Context c) {
    super(c);

    mContext = c;
    setFocusable(true);
    setFocusableInTouchMode(true);

    this.setOnTouchListener(this);

}

public SomeView1(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;

}

public void foodPortionCal() {

    final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle("Output");
    builder.setMessage("Message here");

    builder.setCancelable(true);

    builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            sqLiteHelper.insertDataFood(GlobalVariable.mealTypeF, String.format("%.2f", totalProteinPer) + '%', String.format("%.2f", totalGrainsPer) + '%', String.format("%.2f", totalVegetablePer) + '%', String.format("%.2f", totalFruitPer) + '%', String.format("%.2f", totalDairyPer) + '%', GlobalVariable.dataF);
            Toast.makeText(mContext, "Added", Toast.LENGTH_SHORT).show();
            //this is where I want to set the feature to remove the view/static context?
            FragmentManager fragmentManager = ((FragmentActivity)mContext).getSupportFragmentManager();
            fragmentManager.popBackStack();


            dialog.dismiss();


        }
    });

    builder.show();


  }
}

//Before going the code will move to someview1 we will be in a fragment class called FoodFragment and this is how it look like

public class Food_Cal extends Fragment {

    private void mealTypeSelection()
    {
    //this is how I call the come view activity
            getActivity().setContentView(new SomeView1(view.getContext()));


    }

}

// here is a link to how this app was constructed Android: Free Cropping of Image the only biggest different and the things that is giving me issue is that my app uses fragment and the code the the reference doesn't

The result wanted to get is either it will move to next view with fragment or go all the way back to the root view with fragment. the result I am getting are either the app keep crashing and not able to go back to root view then the app crash this is the error message I am getting

2019-04-27 00:35:36.599 624-624/apd.bii.diabetesappv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: apd.bii.diabetesappv1, PID: 624
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v4.app.FragmentActivity
    at apd.bii.diabetesappv1.FoodCalculation.SomeView1$1.onClick(SomeView1.java:437)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:173)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6894)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
KUROYUKI
  • 113
  • 4
  • 16
  • It's better if you can complete your question by pasting the stack trace of the error that caused your app to crash? – Kidus Tekeste Apr 26 '19 at 16:31
  • @KidusTekeste Thanks I have added the message I receive then the app crash. but I am not sure how to look at this error message. – KUROYUKI Apr 26 '19 at 16:41

1 Answers1

0

This is a simple method used for adding fragment into your FragmentManger so it can be visible. All you need to do is pass in the fragment you want to add on the screen.

public void addScreen(Fragment fragment) {
    getSupportManager()
        .beginTransaction()
        .replace(R.id.container, fragment)
        .addToBackStack(null)
        .commit();
    screensOnTheStack++;
}

And when you want to pop that fragment out of the stack and go to your parent view is

while (screensOnTheStack > 0) {
        fragments.popBackStackImmediate();
    }

Or simply by defining your fragment TAG for determining the root(parent) fragment, like

private static final String BACK_STACK_ROOT_TAG = "root_fragment";

then


    // Pop off everything up to and including the current tab
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.popBackStack(BACK_STACK_ROOT_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    // Add the new tab fragment
    fragmentManager.beginTransaction()
        .replace(R.id.container, TabFragment.newInstance())
        .addToBackStack(BACK_STACK_ROOT_TAG)
        .commit();

Now, for the part you said

The result wanted to get is either it will move to next view with fragment or go all the way back to the root view with fragment.

you can put your own condition as to when you would like to go to the fragment you want to go or either go to your parent view instead.

Kidus Tekeste
  • 651
  • 2
  • 10
  • 28