gpt4 book ai didi

java - 回到调用另一个 Activity 的 fragment

转载 作者:行者123 更新时间:2023-12-01 10:44:55 25 4
gpt4 key购买 nike

我的 MainActivity 有两个 fragment ,例如fragment1 和fragment2。现在,当 Activity 启动时,默认 fragment 是fragment1。然后我导航到fragment2,它又在某些事件更改时创建另一个 Activity ,例如CalledActivity。我想要的是能够在 CalledActivity 中按回键时导航回fragment2 而不是fragment1。我怎样才能做到这一点?

看了几个SO问题,似乎没有明确的答案。

public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

private Toolbar toolbar;

//for display of name and email inside the navigation drawer header
private TextView name_header;
private TextView email_header;
private String nameChar;

AlertDialogManager alert = new AlertDialogManager();

// Session Manager Class
SessionManager session;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Session class instance
session = new SessionManager(getApplicationContext());
session.checkLogin();


setContentView(R.layout.activity_main);


/**
*Setup the DrawerLayout and NavigationView
*/

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);


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

name_header = (TextView) findViewById(R.id.name_header);
email_header = (TextView) findViewById(R.id.email_header);

name_header.setText(Config.USER_FULL_NAME);
email_header.setText(Config.USER_EMAIL);

nameChar= Utils.parseName(Config.USER_FULL_NAME);

TextDrawable drawable = TextDrawable.builder()
.buildRound(nameChar, Color.parseColor("#FF6F00"));

ImageView image = (ImageView) findViewById(R.id.image_view_name);
image.setImageDrawable(drawable);

/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new PrimaryFragment()).commit();


/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();


if (menuItem.getItemId() == R.id.nav_item_product) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new ProductsFragment()).addToBackStack( "products fragment commit" ).commit();

}

if (menuItem.getItemId() == R.id.nav_item_home) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new UploadImageFragment()).addToBackStack( "home fragment commit" ).commit();
}

if (menuItem.getItemId() == R.id.nav_item_order) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new TabFragment()).addToBackStack( "orders fragment commit" ).commit();
}


return true;
}

});


/**
* Setup Drawer Toggle of the Toolbar
*/


ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);

mDrawerLayout.setDrawerListener(mDrawerToggle);

mDrawerToggle.syncState();


}

@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
this.finish();
}
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String stredittext = data.getStringExtra("edittextvalue");
Log.d("xyz",stredittext);
}
}
}





}

最佳答案

您必须像这样重写 CalledActivity Activity 上的 onOptionsItemSelected 方法

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

关于java - 回到调用另一个 Activity 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34245453/

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