I am making a simple application that allows the user to take a photo using the dispatchTakePictureIntent() that saves the full-size photo; indicated here; http://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent. And launched using a Camera icon from the action bar. The user then takes a photo presses ok and their picture appears in a list created using a class extending BaseAdapter
public boolean onOptionsItemSelected(MenuItem item) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
return super.onOptionsItemSelected(item);
}
If I launch the intent as above then everything seems to work fine but I have no response to the delete all button (clicking it would ofc just start the camera intent)
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_photo : {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
}
case (R.id.action_delete) : {
deleteAllPhotos();
}
}
return super.onOptionsItemSelected(item);
}
However when setup like this I can delete the photos, but the previously taken photo dissapears every time I click the camera icon to take a new photo
Is there any subtle change in lifecycle or anything introduced by this difference? As I am struggling to find how it can cause this different behaviour.
Thanks for any input!
You're missing a break or return statement after your case, so it launches the intent and then deletes all of your previous photos since case statements fall thru to the next one if not told to break.
Try:
switch(item.getItemId()) {
case R.id.action_photo:
...
break;
case R.id.action_delete:
...
break;
}
Related
Below link in settings activity cannot go back when back button is pressed.
https://github.com/SadaqaWorks/Word-By-Word-Quran-Android/blob/master/app/src/main/java/com/sadaqaworks/quranprojects/activity/SettingsActivity.java
Code is given below...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)`enter code here`
}
return super.onOptionsItemSelected(item);
}
nothing gets work. plz fix the code.
You may want to use finish() instead of System.exit(0).
And do not override onBackPressed().
How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference
I'm new to Android and I trying to do a simple app.
In the "home", I write my name and press the "send" button.
In the second page, the app show "Hello " + inserted name.
The problem is:
When I touch the back virtual button in nav bar, I come back to the home and in the input field there is always the name; but when I go back with the back button in header bar, the input field is empty.
I've tried with:
in onOptionsItemSelected:
switch (item.getItemId()){
case android.R.id.home:
Log.d("case","go back");
NavUtils.navigateUpFromSameTask(this); //or with finish();
return true;
}
it prints "case: go back" in console, but doesnt' work correcly.
My custom theme have as parent Theme.AppCompat.Light.DarkActionBar and if i put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreate, Android Studio tells me "Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'".
I solved adding this in the second Activity:
#Override
public Intent getSupportParentActivityIntent() {
return null;
}
and edit the switch/case function like this:
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return false;
default:
return super.onOptionsItemSelected(item);
}
In my Android project user is clicking few times at button so I want to block that button if it's pressed already. Now SaveOrder() method is fired few times which is not acceptable.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.order_save:
Log.d("Custom Synchronization", "Click!");
SaveOrder();
canQuit = true;
hasAcceptQuit = true;
onBackPressed(); // navigate to other acitivty
return true;
case R.id.order_adnotation:
setAdnotationOrder();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You could use a boolean that starts with false like:
private static boolean hasPressed = false;
And then when calling the SaveOrder method you check for the boolean to be false and change it:
if(!hasPressed) {
SaveOrder();
hasPressed = true;
}
Then, after the execution of the method, you could change the boolean again so the user can call the method again. Be aware that the animation of click will still happen, so consider showing something like a Toast to the user to inform what's going on.
Action Bar
I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. It is automatically defined when we select the "Black activity" template.
My app has a pretty huge hierarchy graph, got about 25 activities now.
I'm basically just showing a few tutorials and one can navigate to it according to the categories.
Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. The code shows no error, but when I actually press that button, the app stops working.
What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image.
When I press it, the top most screen should close, and the last one should open. Just close the screen.
What I tried :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is the the function that inflates that buggy button along with the action bar. I tried to replace the entire code, and call "Finish" function, but that failed miserably.
I was unable to find a function specifically made for that top left most button...
I want the top most screen on the stack(the one in the foreground) to close, when this button is touched.
How to do this ?
I think the easiest way out is follows:
I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This should finish your current activity. However if you have lot of activities, then you might have to do this in all the activities. To save yourself from that effort, you can make a class lets call it AbstractActivity; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. Basically this sort of thing (Inheritance)can be used any time, when there are some common features which would be applicable to your many classes.
If you are receiving any errors, please do post your LogCat if you require further help.
Hope this helps you.
just giving basic code given by #shobhit puri...
for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected....
protected void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}