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)