When i logout my app (press logout menu button) from second activity, then it redirect to login page but when i pressed back button again show second screen Activity, it should be goto main screen on back pressed, because i use finish() method but still not go to main screen. then what i do?
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second_adapter, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout_sec_Act) {
session.logoutUser();
finish();
Toast.makeText(Second_activity.this, "Logout...", Toast.LENGTH_LONG)
.show();
}
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
That is because your previous activity is still in the back stack of your task. One way to solve it is by moving to a new empty task when you logout, something like this:
Intent logoutIntent = new Intent(this, MainActivity.class);
logoutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(logoutIntent);
For the first time i use the contextual actionbar. When i click in the item of my ListView i want have the possibility to share that item in some way (whatsapp/mail/gmail/drive etc etc..). I'm trying in with this method but no works:
the menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/>
</menu>
The contextual actionbar class:
public class CABMode implements ActionMode.Callback {
private ShareActionProvider mshare;
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// don't need to do anything
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
Mode = null;
}
public Intent Share() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Share");
return intent;
}
}
Well, the contextual actionbar it opens well when i do an long click, but the share button in the actionbar doesn't work. When i say "not works" i mean that seems not clickable. Nothing works when i click over the item. Nothing appears. What i want it's open the multiple share ways. Thanks
When i say "not works" i mean that seems not clickable
ActivityChooserView, the action view ShareActionProvider returns, disables the "share" button when there are no items to display, which in your case if because you're setting up ShareActionProvider incorrectly. All of the "on click" events are handled internally, you won't receive any callbacks to ActionMode.Callback.onActionItemClicked.
Rather than settings it up in ActionMode.Callback.onActionItemClicked, move that code into ActionMode.Callback.onCreateActionMode and call Menu.findItem to initialize it.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.cab, menu);
final MenuItem item = menu.findItem(R.id.your_share_action_provider_id);
mshare = (ShareActionProvider) item.getActionProvider();
mshare.setShareIntent(Share());
return true;
}
I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. How would I achieve this? At the moment I have it working for just a single short press/click using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// TODO: Only onlongclick should the menu be revealed
getMenuInflater().inflate(R.menu.my_menu_id, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). I've looked into onLongClickListener but can't get it to work. Thanks for your help! Really appreciate it.
I ended up figuring this one out myself. There's 2 ways of doing it. Either a context menu or a alert dialog menu. The alert dialog menu allow icons whereas the context menu does not.
See my answer here:
Open Actionbar Menu on Longclick - Android
I'm using sherlock action bar.
I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.
This is my code on Java
#Override
public boolean onPrepareOptionsMenu (Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menutes, menu);
todaySched=menu.findItem(R.id.todaySched);
if(todaySched.isEnabled()){
todaySched.setIcon(R.drawable.calendarselected);
}
return true;
}
but when I do this the icon become double, and the icon won't change neither.
Can someone help?
Use the on onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.todaySched:
// put your code here to change the icon
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...