[英]Action bar on Action back (Navigation Up) to custom activity?
我的申请中有很多活动
因为我已经给出了
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
从其他活动返回首页
像下面
如果我按下这三个按钮,所有菜单都在工作,这就是我的“选项”菜单
@Override 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.login
或id == R.id.home
但是要登录几天才能返回首页活动
但是,如果我按Back ..,返回的操作将重定向到“登录”主页
我已经使用共享首选项为我的应用程序添加了一个登录页面..现在它是启动器活动。
在“我的登录”活动中,如果用户一旦登录,它应该每次都重定向到“主页”活动。
但是在操作栏上,当我按箭头按钮时,它会重定向到空的登录页面。
更新
如果登录凭据成功重定向到应用程序启动时的“主页”活动,我也会给出“给定意图”,它将每次检查一次
除了采取行动外,一切都很好
如何解决这个问题...
好的,请确保您确实声明了以下活动-
<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
只需重写此方法。
@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.