【问题标题】:Action bar on Action back (Navigation Up) to custom activity?操作返回(向上导航)到自定义活动的操作栏?
【发布时间】:2016-01-19 05:37:54
【问题描述】:

我的应用程序中有很多活动

因为我已经给出了这个

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

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
}

从其他活动返回主页

喜欢下面

如果我按下这三个按钮,所有菜单都可以正常工作 这是我的选项菜单

@覆盖 public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.abc) {
    Intent mypIntent = new Intent(this, abc.class);
    startActivity(mypIntent);
    return true;
    }

else if (id == R.id.web) {
    Intent webIntent = new Intent(this, Web.class);
    startActivity(webIntent);
    return true;
}
else if (id == R.id.about) {
    Intent aboutIntent = new Intent(this, About.class);
    startActivity(aboutIntent);
    return true;
}
.
.
.
..

return super.onOptionsItemSelected(item);
}

这里没有名为 id == R.id.loginid == R.id.home 的菜单,但它会在几天前登录它的回家活动

但是如果我按返回.. 操作返回将重定向到登录页面 Inst-ed of Home

我使用共享首选项为我的应用程序添加了一个登录页面。现在它是启动器活动。

在我的登录活动中,如果一旦用户登录,它应该每次都重定向到主页活动.. 并且工作正常..

但在操作栏上,当我按下箭头按钮时,它会重定向到空的登录页面.. 如果我按取消整个应用程序正在运行..我的凭据是安全的,除了这个操作栏..

更新

如果登录凭据成功重定向到应用程序启动时的主页活动,我也有给定意图,它每次都会检查

一切都很好,除了行动回来

如何解决这个问题...

【问题讨论】:

  • 你的 HomeActivity 上有返回按钮吗?
  • 主页活动中没有后退按钮.. 我还有更多其他活动.. 就像关于 ect 的网站...如果我按下它重定向到登录的操作栏中... ..在添加登录页面之前它会回家......但现在它重定向到启动器,即......登录......
  • wt是app的最小sdk吗?

标签: android menu


【解决方案1】:

好的,请确保您声明如下活动 -

<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- The meta-data element is needed for versions lower than 4.1 -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

现在在每个活动中添加下面的代码块-

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

更新 添加一个新的类文件来检查登录或不使用 home 作为默认值.. 并将您的新类替换为 Manifest 中的 launcher

【讨论】:

  • 是的,我已经在清单中给出了这个.. 几天前它工作得很好.. 但现在它重定向到启动器活动.. 请参阅我想要在操作栏返回而不是选项菜单的更新问题..
  • @Don't Benegative: 那么android按钮rt?然后你在 onOptionItemSelected() 中得到它的 onclick ,所以这是有效的方法。
  • 但我想要后退按钮..不是选项按钮
  • @Don'tBenegative: 好吧,Actionbar 后退按钮
  • @Don'tBenegative:你没有完成你的 LoginActivity 吗?
【解决方案2】:

只需覆盖此方法。

 @Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
        {
             Intent intent = new Intent(mContext, Activity.class);/*your activity name*/
        startActivity(intent);
        }
        default:
            return super.onOptionsItemSelected(item);
    }
}

【讨论】:

  • 没关系,但我已经给了这个if (id == R.id.web1) { Intent webIntent = new Intent(this, Web.class); startActivity(webIntent); return true; } else if (id == R.id.about1) { Intent aboutIntent = new Intent(this, About.class); startActivity(aboutIntent); return true; }我需要后退箭头按钮,,
  • 无论您想重定向什么活动,只需将 Activity.class 替换为您自己的类
  • 你不应该再次开始活动,因为它已经在堆栈上
  • 你给菜单不返回按钮检查我的更新问题
  • 一旦你声明“actionBar.setDisplayHomeAsUpEnabled(true);”您可以使用我提供的方法访问后退按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多