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);
}
}
}
Related
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:)
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 have an Navigation Drawer (MainMenu.class config)
First MainMenu load NewRadioFragment then i click button at NewRadioFragment start to ViewPager, ViewPager have (PlayRadioFragment and BlogContentFragment) View through ViewPagerActivity ( FragmentActivity ).
Now, I want Back Button in Actionbar back from PlayRadioFragment to NewRadioFragment.
I need help !.
Update
private void setImageView(ImageView src, View rootView) {
image_disk = (ImageView) rootView.findViewById(R.id.base);
new Thread(new Runnable() {
#Override
public void run() {
final Bitmap bimit = Utils.loadBitmap(image_url);
image_disk.post(new Runnable() {
#Override
public void run() {
image_disk.setImageBitmap(bimit);
prg_loading.setVisibility(View.GONE);
tv_loading.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
}
});
}
}).start();
}
When i click function don't finish
have you tried this?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
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);
}
or
try this instead
#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);
}
or
call
Finish();
I have an activity menu and on one fragment I want to replace an item inside that menu. The item when pressed launches an activity. My fragment menu item works, but it also calls the activity menu item intent. I need to remove the duplicate main activity menu item being selected too.
Main Activity menu:
public boolean onOptionsItemSelected(MenuItem item) {
else if(itemId == R.id.action_settings)
startActivity (new Intent(getApplicationContext(),
PreferencesActivity.class));
return super.onOptionsItemSelected(item);
}
Fragment menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getActivity(),
PreferencesFragment.class));
return false;
default:
break;
}
return false;
}
It is only if the activity doesn't use the action that it is passed to the fragment. You may be able to add in the activity a boolean that causes the switch to be bypassed for that item.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!isMyFragmentPresent()) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(),
PreferencesActivity.class));
return true;
default:
break;
}
}
return false;
}
public void setMyFragmentIsPresent(boolean isMyFragmentPresent) {
this.isMyFragmentPresent = isMyFragmentPresent;
}
UPDATE: I realise this doesn't work if you have more items than the one you want to change. Maybe you would be better having a default Fragment that loads in the activity that does nothing but load the R.id.action_settings settings button and what it does. This fragment would then be replaced by your new Fragment.
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;
}
>