简体   繁体   English

返回到自定义活动的“动作”(导航向上)上的动作栏?

[英]Action bar on Action back (Navigation Up) to custom activity?

I have many activities in my application 我的申请中有很多活动

in that I have Given this 因为我已经给出了

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

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

to Go back to Home Page from other activities 从其他活动返回首页

like Below 像下面

在此处输入图片说明

If I press those three buttons all menus are working this is my Option menu 如果我按下这三个按钮,所有菜单都在工作,这就是我的“选项”菜单

@Override public boolean onOptionsItemSelected(MenuItem item) { @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);
}

Here There is no menu named id == R.id.login or id == R.id.home but its going to login few days back its gone to home activity 此处没有名为id == R.id.loginid == R.id.home但是要登录几天才能返回首页活动

but If I press Back.. action back is redirect to Login page Inst-ed of Home 但是,如果我按Back ..,返回的操作将重定向到“登录”主页

I have added a Login page for my application using shared preferences.. and it is now launcher activity.. 我已经使用共享首选项为我的应用程序添加了一个登录页面..现在它是启动器活动。

Here In my Login activity on if once user is sign in it should it should redirect to Home activity on every time.. and its working fine.. 在“我的登录”活动中,如果用户一旦登录,它应该每次都重定向到“主页”活动。

But on action bar when I press arrow button it is redirecting to empty login page.. if I press cancel entire app is working .. my credentials are safe except this action bar.. 但是在操作栏上,当我按箭头按钮时,它会重定向到空的登录页面。

Update 更新

I have Given Intent also if Login credentials success redirect to Home activity on app start up it will check every time 如果登录凭据成功重定向到应用程序启动时的“主页”活动,我也会给出“给定意图”,它将每次检查一次

every thing is as for fine except action back 除了采取行动外,一切都很好

how to fix this... 如何解决这个问题...

Alright make sure u do declare activities as below - 好的,请确保您确实声明了以下活动-

<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>

Now in every activity add below code block- 现在在每个活动中添加以下代码块-

@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);
}

Update add a New class file to check login or not else use home as default.. and replace your new class as launcher in Manifest 更新添加一个新的类文件以检查登录名,否则不使用home作为默认值。并替换您的新类作为Manifest launcher

Just override this method. 只需重写此方法。

 @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM