Android ActionBarSherlock On Top Icon Back - android

I'm trying to make my top action bar icon to allow users to go back to previous screen. I tried to implement these codes. But none are working. Can anyone please guide me on this. I know this looks simple, I'm new to android . Below are my codes.
Problem : When i tap on the icon button it just cleared my screen without going to the previous screen.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
checkInternetConnection();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<--THIS
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, SingleViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

This is the way I do it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}

In your ressources(res)
go to menu
and add
this make sur u have some button back for in your drawable
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/back"
android:icon="#drawable/back1"
android:showAsAction="always|withText"
android:title="back"/>
now in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case R.id.back:
Intent in = new Intent(this, <classname which you want to go back>.class);
startActivity(in);
break;
}
return false;
}

you may try this code
<item
android:id="#+id/back"
android:icon="#drawable/btn_back"
android:showAsAction="always|withText"
android:title="#string/txt_back"/>
>
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case R.id.back:
back_action();
return true;
default:
break;
}
return true;
}
>

Related

Option menu option not directing to another activity

I want to navigate to another Activity when click on the option menu item 'settings' from the menu bar. Nothing actual happens.I have checked similar issues posted here, but i can understand why this is not working for option menu.
see the code below:
Can't go to a new activity from selected option from option menu
<item
android:id="#+id/mySettings"
android:title="#string/action_settings" />
<item
android:id="#+id/logout"
android:title="log out" />
code:
public class Dashboard extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_bar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
public void openConfigure(){
Intent intent = new Intent(this,Configure.class);
this.startActivity(intent);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Use onOptionsItemSelected instead of onContextItemSelected cause you are using OptionMenu not ContextMenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return super.onOptionsItemSelected(item);
}
to select an options menu item, you have to override onOptionItemSelected() :
try below code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return true;
}

Android menu crashs application

I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent("com.my.project.BAAS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please help, 2 days so far.....
You have to pass context and class which is to be opened.
Your code should be like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent(getContext(),BAAS.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hope it helps:)

Opening new activities from Popup menu items

I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?
Following are my code snippets.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi") #Override
public boolean onOptionsItemSelected(MenuItem item) {
View menuItemView = findViewById(R.id.action_button);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup);
popupMenu.show();
return true;
}
and my popup menu is as follows,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/one"
android:title="About"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
<item
android:id="#+id/two"
android:title="Contact Us"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
</menu>
What i want to do is, when i click on these menu items another activities has to be opened. How could i do that?
Can someone help me please. Thanks in advance.
Use the id to launch the activity using switch statement with menu itemId
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.one:
Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
startActivity(intent1);
return true;
case R.id.two:
Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
To open activity on popup menu click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Android StartActivity option menu

I'm trying to start an activity from OptionsMenu, but it doesn't start. Why?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.main:
Intent intent = null;
intent = new Intent(MainActivity.this, Impostazioni.class);
this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't you end up returning early when you do?
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
You never reach the code for startActivity I think. If that's not the case, are you sure the menu item has id R.id.main? I would want to debug this code with a break point at the top of this method - then step through and see what gets called and what doesn't.
// Delay is in milliseconds
static final int DRAWER_DELAY = 200;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//open navigationDrawer
new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY);
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.main:
Intent intent = null;
intent = new Intent(MainActivity.this, Impostazioni.class);
this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Method to open the Navigation Drawer...
private Runnable openDrawerRunnable() {
return new Runnable() {
#Override
public void run() {
mDrawerToggle.openDrawer(Gravity.LEFT);
}
}
}

why it don't work when I click back arrow icon?

I set the following code in my activity:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Why it can't work, when i click back arrow icon?
You need implement it by yourself:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Categories

Resources