I have a Main Activity which contains two Fragment A(Tab1) and B(Tab2). From B(Tab2), i have created a activity(CategoryBlogs). Now i want to go back from activity (CategoryBlogs) to Tab2. but when i presses the NavUp button on custom ActionBar , nothings happens
CategoryBlogs.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category__blogs);
listView = (ListView) findViewById(android.R.id.list);
Toolbar tool_bar1 = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(tool_bar1);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String message = intent.getStringExtra(Tab2.EXTRA_MESSAGE);
String link = message.trim() + "?feed=titles";
Log.d("ye category click krne par next activity me ye link bnta hy parsing ke liye",link);
loadPage(link);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_category__blogs, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.d("hum option item selected me a gae", id+"");
if(id==android.R.id.home){
NavUtils.navigateUpFromSameTask(this);
Log.d("hum match kr gae", id+"");
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
AndroidManifest.XML
<activity
android:name=".Category_Blogs"
android:label="App For Blog" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.talha.test_fragement.MainActivity"
/>
</activity>
</application>
add in onCreate
setHasOptionsMenu(true);
and debug onOptionsItemSelected is it execute or not..
You haven't specified the android:parentActivityName to which you are supposed to Navigate up in the AndroidManifest.xml file under activity tag.
android:parentActivityName="com.example.myfirstapp.MainActivity" >
Here's everything you need to know about Navigating Up
1. Specify the Parent Activity in manifest.xml
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
2. Add Up Action in Activity (For the ActionBar <-)
getActionBar().setDisplayHomeAsUpEnabled(true);
3. Navigate up to Parent by overriding onOptionsItemSelected() method
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);
}
For more information, follow this link http://developer.android.com/training/implementing-navigation/ancestral.html
You have to get parent activity and
You have to replace this block of code -
NavUtils.navigateUpFromSameTask(this);
with this:
Intent intent = NavUtils.getParentActivityIntent(this); //get parent activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent); // and navigate your current activity to your parent activity
You've also tried to change the launchMode in the manifest at the <activity> element to
android:launchMode="singleTop"
or
android:launchMode="singleTask
There can be many solutions to solve your problem.
Related
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);
}
}
I'm playing around with Android Studio so I created a SettingsActivity using the wizard and I'm faced with the problem that it is not possible to navigate from this settings activity back to the main activity using the "up" arrow in the Actionbar.
The setup of the Actionbar looks like this:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Actionbar is not null btw.
And the parentActitvityName is set in the AndroidManifest:
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.demo.app.MainActivity" />
</activity>
However, a click on the arrow does nothing. Not even onOptionsItemSelected gets triggered.
Seems like this is exactly the same problem
Action bar setDisplayHomeAsUpEnabled not working on ICS but navigating back from a detail to an overview activity is working fine in the very same app. Moreover I set MinSDK to 15 and TargetSDK to 23.
override the onOptionsItemSelected method on your AppCompatPrefernceActivity and make it like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Vspallas answer is correct. The mistake was on my side. I had an onOptionsItemSelected method inside the preferenceFragment, not in the Activity. Mea culpa.
You can use this inside the settings activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then in androidManifest is the same as that you have did
<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity"
android:label="#string/title_activity_settings">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.MainActivity" />
</activity>
this com.example.app_name is you App package name
I have 2 activities: HomeActivity and EmailChangeActivity and some fragment inside HomeActivity. What I want to do is to set back navigation arrow in EmailChangeActivity toolbar. I have more activities and somehow I managed to make that arrow (but I made Intents straight from activities, not from fragment inside activities). What I did in order to get that arrow was:
When I go to EmailChangeActivity from HomeActivity I call this in HomeActivity fragment:
Intent intent = new Intent(getActivity().getApplicationContext(),
ChangeEmailActivity.class);
getActivity().startActivityForResult(intent, 1);
Inside EmailChangeActivity I insert standard code creating Toolbar and setting it:
// Setting Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.change_email_activity_name));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
In Manifext.xml I made:
<!-- Home Activity -->
<activity
android:name="com.example.nazwamarki.myapplication.HomeActivity"
android:label="#string/home_activity_name"
android:theme="#style/AppTheme.Normal">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Change Email Activity -->
<activity
android:name="com.example.nazwamarki.myapplication.ChangeEmailActivity"
android:label="#string/register_activity_name"
android:theme="#style/AppTheme.Normal"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.nazwamarki.myapplication.HomeActivity" />
</activity>
Still got no result. Any help?
Edit
I forgot to add that I also included in EmailChangeActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
If you're not using an ActionBarActivity:
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// The intent to go back to HomeActivity
}
});
If you are using an ActionBarActivity, then all you need to do is make Android use the Toolbar as an ActionBar.
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Looks like you're missing this part:
#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);
}
You can get more details on this here Android Developers- Providing up navigation
So I have a very simple project. It contains two activities. The main activity has a navigation drawer and a fragment container. The second activity is merely meant to display details when the user interacts with a certain fragment.
So I have set my main activity as the parent activity to my second activity (called DetailsPage) like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsPage"
android:label="#string/title_activity_details_page"
android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.collusion.srviceassistant.MainActivity"/>
</activity>
and in the DetailsPage activity code I have the following:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.share :
share();
return true;
case R.id.home:
Log.i("BACK", "Going back!");
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new
// task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Now it seems that by my log, the code that pertains to the up action is not getting executed. Whenever I hit the up caret in the action bar or use the back hardware key, the app simply exits. Does anyone have any idea what I am doing wrong here? I am wondering if it has anything to do with the fact that the details page activity does not extend the ActionBar class:
public class DetailsPage extends Activity{
Does anyone have any ideas?
According to Google docs link on navigation using the home button you might want to use
android.R.id.home instead of R.id.home
#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);
}
I think this might solve the home up navigation.
I have been trying to add a back button to the action bar.
I want my view to look like this:
I want to add the back button in the left of the action bar.
I added this code
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
but it doesn't work.
How can I fix this?
After setting
actionBar.setHomeButtonEnabled(true);
Add the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Add
actionBar.setHomeButtonEnabled(true);
and then add the following
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.
this one worked for me:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.
After setting
actionBar.setHomeButtonEnabled(true);
You have to configure the parent activity in your AndroidManifest.xml
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="#style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html
There are two ways to approach this.
Option 1: Update the Android Manifest
If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar
<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>
Option 2: Change a setting for the ActionBar
If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your #imports match the level of compatibility you are looking for).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Then, detect the 'back button' press and tell Android to close the currently open Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That should do it!
Firstly Use this:
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
Then set operation of button click in onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method
Duplicate of Android Sherlock ActionBar Up button
Simpler and better:
For API >= 16
Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
Use this to show back button and move to previous activity,
final ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if anyone else need the solution
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
Add this line in onCreate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then Override this method
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}