gpt4 book ai didi

android - MainActivity 在按下后退按钮时分段并返回 MainActivity

转载 作者:行者123 更新时间:2023-11-29 15:43:44 28 4
gpt4 key购买 nike

我有一个 Mainactivity,其中包含一个 Layout,它是 4 个子布局的父级。单击子布局后,我将使用一个新 fragment 替换主布局。但我无法在按下返回按钮后返回 MainActivity

主 Activity .java

public class MainActivity extends AppCompatActivity {

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

aboutUs = (RelativeLayout) findViewById(R.id.aboutUs);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}


//click methods goes here

public void clickAboutUs(View view){

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentAboutUs fragmentAboutUs = new FragmentAboutUs();
fragmentTransaction.replace(R.id.fragment_container,fragmentAboutUs);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.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.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


}

关于我们的 fragment .java

public class FragmentAboutUs extends Fragment
{

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

View view = inflater.inflate(R.layout.about_us, container,false);
return view;
}
}

如何在 fragment 中按下返回按钮后再次返回主页面。

最佳答案

像这样尝试

    public boolean popFragment() {
boolean isPop = false;

Fragment currentFragment = getSupportFragmentManager()
.findFragmentById(R.id.flContent);

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getSupportFragmentManager().popBackStackImmediate();
}

return isPop;
}

@Override
public void onBackPressed() {
if (!popFragment()) {
finish();
}
}

public void replaceFragment(Fragment fragment, boolean addToBackStack) {

FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();

if (addToBackStack) {
transaction.addToBackStack(null);

} else {
getSupportFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);

}
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();

}

添加上面的方法是在你的父 Activity 中并像这样使用

 FragmentAboutUs fragmentAboutUs = new FragmentAboutUs();
replaceFragment(fragmentAboutUs , true);

关于android - MainActivity 在按下后退按钮时分段并返回 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326776/

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