简体   繁体   中英

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

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

but If I press Back.. action back is redirect to Login page Inst-ed of Home

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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