用片段开始活动

时间:2017-03-13 13:35:38

标签: android android-fragments

我有NavigationDrawerActivity名为MainActivity。我有另一个活动,显示为Mainctivity GuestActivity上的MainActivity对话框。

我有两个用HomeFragment打开的片段; RegisterFragmentGuestActivity

我的GuestActivity中有一个按钮,现在点击该按钮时,我希望MainActivity关闭,然后在加载RegisterFragment的情况下打开 protected $deletedAtCol = "deleted_at"; /** * Get the name of the "deleted at" column. * * @return string */ public function getDeletedAtColumn() { return $this->deletedAtCol; } /** * Set the name of the "deleted at" column. * @param string $colName * @return string */ public function setDeletedAtColumn($colName) { $this->deletedAtCol = $colName; } 。< / p>

有没有办法做到这一点,如果是这样的话怎么样?我可以看一个例子吗?

2 个答案:

答案 0 :(得分:2)

您必须使用GuestActivity启动startActivityForResult。这样,您可以通过单击按钮设置结果,并相应地更改MainActivity中的片段,而无需再次启动MainActivity。

在MainActivity中更改为:

    startActivityForResult(guestIntent, 1337);

并覆盖onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1337) {
    // Make sure the request was successful
    if (resultCode == RESULT_OK) {


        // Change framgnet
    }
}
}

在GuestActivity中按钮的onClickListener中添加:

setResult(Activity.RESULT_OK);
GuestActivity.this.finish(();

答案 1 :(得分:1)

当您按下按钮然后拨打以下电话:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("start_register_fragment", true);
startActivity(intent);
finish();

现在在MainActivity onCreate方法检查

if(getIntent.getExtras.getBoolean("start_register_fragment")) {
//call to register fragment here
}