I have activity ActivityProfile, have getSupportActionBar().setDisplayHomeAsUpEnabled(true); implemented, onBackPressed() as well, search all over the internet and still no help.
#Override
public void onBackPressed() {
Toast.makeText(this, "OnBackpressed fired", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode== KeyEvent.KEYCODE_BACK) {
// something here
onBackPressed();
}
return true;
}
using the device's back button works, but not on the app...
To implement Up navigation, declare a parent of that particular activity in manifest and setDisplayHomeAsUpEnabled as true.
<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>
Read More https://developer.android.com/training/implementing-navigation/ancestral.html
you should use onOptionItemSelected to handle click on the bottom on action bar :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
This should work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
hi add this code in your activity. when you press back button in toolbar following code execute.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
Related
I was trying to intent The getSupportActionBar() to the previous page.
While creating a back arrow to toolbar , I am getting an error for the second method says error: cannot find symbol class MenuItem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override // This method creates the error
public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(StartActivity.this, MainActivity.class));
}
Try This to get click event of back arraow
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// perform Your action here
return true;
}
return false;
}
and in your manifest file add parent activity to your activity like this
<activity
android:name=".yourActivity"
android:parentActivityName=".ParentActivityname"/>
You can try this
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
#Override // This method creates the error
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(StartActivity.this, MainActivity.class));
return true;
}
return false;
}
Add the meta-data tag in your AndroidManifest.xml inside tag like this
<activity
android:name=".StartActivity"
android:parentActivityName="your package name.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="your package name.MainActivity" />
</activity>
Note: Change "your package name" to your project package name
Ratilal Chopda already posted the right answer, here is only the solution in pretty code
public class ServicesViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// etc...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file
<activity android:name="com.example.ServicesViewActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
</activity>
http://developer.android.com/design/patterns/navigation.html#up-vs-back
You could also check if menu item was imported properly
that was my own experience the class wasn't imported hence it couldn't find the MenuItem
I have two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In my application, I defined 2 activities in the Manifest file like this:
<activity android:name=".event.EventDetailsActivity"
android:launchMode="singleTop"
android:parentActivityName=".main.MainActivity"
android:theme="#style/AppTheme.WithActionBar"/>
<activity
android:name=".main.MainActivity"
android:launchMode="singleTop"/>
In the MainActivity, I have 4 fragments. In one fragment, I start the EventDetailsActivity in one fragment using:
Intent intent = new Intent(new Intent(getContext(), EventDetailsActivity.class));
intent.putExtra(EventDetailsActivity.ID_KEY, id);
intent.putExtra(EventDetailsActivity.TYPE_KEY, true);
startActivityForResult(intent, DETAILS_REQUEST);
How I handle the back navigation in EventDetailsActivity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onSupportNavigateUp() {
Log.d(TAG, "Navigate up");
onBackPressed();
return true;
}
#Override
public void onBackPressed() {
Log.d(TAG, "Back pressed " + isChanged);
if (isChanged) {
Log.d(TAG, "Set result");
setResult(RESULT_OK);
finish();
}
super.onBackPressed();
}
The problem is onActivityResult get called in the calling Fragment but the result code is always 0 (RESULT_CANCELED). Also, onSupportNavigateUp never gets called if I press the back arrow button. Is there any workaround for this problem?
Remove : android:launchMode="singleTop"
and remove super.onBackpressed in on onBackpressed function
because you used finish()
As you're getting the onActivityResult callback from your Fragment correctly I assume the problem is about setting the proper result to the starting Activity. So here's I'm proposing some edit you might consider in your code.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
setResult(RESULT_OK);
return true;
}
return super.onOptionsItemSelected(item);
}
// No longer necessary
/* #Override
public boolean onSupportNavigateUp() {
Log.d(TAG, "Navigate up");
onBackPressed();
return true;
} */
/* #Override
public void onBackPressed() {
// Super call needs to be the first line
super.onBackPressed();
if (isChanged) {
setResult(RESULT_OK);
}
} */
So I've omitted the onBackPressed function because by default, if the user presses the back button, the result will be Activity.RESULT_CANCELED.
So there's a hacky solution here is to implement the key event callback listener like this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setResult(RESULT_OK);
finish();
}
return super.onKeyDown(keyCode, event);
}
Hope that helps!
Refer Below Image,
I want to go back to the previous activity.
But on clicking the back button on toolbar,Nothing is happening.
I have used the following code to achieve that still no luck.
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId() == R.id.home)
{
finish();
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In your OnCreate method
toolbar.setTitle(R.string.title_activity_setting);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In your onOptionsItemSelected method
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I will work.
Add onBackPressed() method on your Activity. And super this. And when click back button call to this.onBackPressed().
Update code for this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
I create other activity and hope use toolbar back arrow to return main activity, but under code doesn't work, please help me.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(R.string.setting);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("??");
finish();
return true;
}
return true;
}
});
}
Try This Method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
return true;
}
return (super.onOptionsItemSelected(menuItem));
}
The Toolbar has a method for this, setNavigationOnClickListener. It allows you to listen for click events on the back arrow. Here is the documentation if you want to read more about the Toolbar.
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("??");
finish();
}
});
You should use this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}