gpt4 book ai didi

java - 返回第一个 View / Root View 的内容

转载 作者:行者123 更新时间:2023-12-02 06:16:05 28 4
gpt4 key购买 nike

嗨,我是 Android 新手,如果有人能够帮助我解决这个问题,我将不胜感激。现在我正在尝试返回到上一个 View 或 Root View ,有谁知道如何从静态上下文返回到上一个 View (我不确定静态上下文是正确的说法,如果有的话请随时纠正我我错了)?

我已经尝试创建 FragmentManager,我使用它的原因是因为我尝试创建的应用程序使用了 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()));


}

}

//这是此应用程序构建方式的链接 Android: Free Cropping of Image唯一最大的不同和给我带来问题的是我的应用程序使用 fragment 而引用不使用的代码

想要得到的结果是要么移动到带有 fragment 的下一个 View ,要么一路回到带有 fragment 的 Root View 。我得到的结果是应用程序不断崩溃并且无法返回 Root View 然后应用程序崩溃这是我收到的错误消息

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)

最佳答案

这是一个简单的方法,用于将 fragment 添加到 FragmentManger 中,使其可见。您所需要做的就是传入要在屏幕上添加的 fragment 。

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

当您想要将该 fragment 从堆栈中弹出并转到父 View 时

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

或者简单地通过定义 fragment 标签来确定根(父) fragment ,例如

private static final String BACK_STACK_ROOT_TAG = "root_fragment";

然后


// 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();

现在,对于你所说的部分

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.

您可以设置自己的条件,决定何时转到您想要转到的 fragment ,或者转到您的父 View 。

关于java - 返回第一个 View / Root View 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55871532/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com