gpt4 book ai didi

android - 如何通过单击返回(不是硬件后退按钮)返回到上一个 fragment ?

转载 作者:行者123 更新时间:2023-11-29 14:21:21 25 4
gpt4 key购买 nike

我只有 1 个 Activity ,其中有 2 个 fragment 。启动 Activity 时, fragment A 将启动。在用户输入某项并单击按钮后,该按钮将定向到 fragment B。所以我想做的是通过在左上角添加一个像这样的“后退箭头”某物 <- 在左上角。

是否与FragmentManager.popBackStack()有关?请指教!谢谢

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
first first=new first();
transaction.add(R.id.top,first);
transaction.commit();
}
}

first.java

public class first extends Fragment implements View.OnClickListener{

Button get_button;
EditText get_input_name;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_first,container,false);
get_input_name=(EditText)rootView.findViewById(R.id.input_name);
get_button=(Button)rootView.findViewById(R.id.submit);
get_button.setOnClickListener(this);
return rootView;
}

public void onClick(View v){

FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
two Two=new two();
Bundle bundle = new Bundle();
bundle.putString("input_name_value",get_input_name.getText().toString());
Two.setArguments(bundle);
transaction.replace(R.id.top,Two);
transaction.commit();

}
}

two.java

public class two extends Fragment implements View.OnClickListener {
TextView get_display_input;
ImageView get_back_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

get_display_input=(TextView)getActivity().findViewById(R.id.display_input);
get_back_button=(ImageView) getActivity().findViewById(R.id.back_button);
Bundle bundle = getArguments();
String get_name = bundle.getString("input_name_value");
//int store_get_input=Integer.parseInt(get_name);
get_display_input.setText("You have entered "+get_name);
}

public void onClick(View v){

// WHAT TO DO HERE IN ORDER TO RETURN TO PREVIOUS Fragment when clicking the button?

}
}

enter image description here

更准确的说,请引用截图。

最佳答案

您可以使用 popBackStack() 来做到这一点FragmentManager 的方法,将它放在后退按钮的 onClickListener 中:

if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}

如果您正在使用工具栏的默认后退按钮,即主页按钮,那么您可以通过将这段代码放在 Activity< 的 onOptionsItemSelected() 方法中来实现:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}
return true;
}

return super.onOptionsItemSelected(item);
}

或者,如果您希望在按下硬件后退按钮时有相同的行为,那么在您的 Activity 类中覆盖 onBackPressed() 方法,如下所示:

@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

关于android - 如何通过单击返回(不是硬件后退按钮)返回到上一个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38438619/

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