How to temporarily block the ability to click a menu item - java

I have a menu when I click on a specific element of which the activity opens, but the user can quickly click menu iten 2 times, which will lead to the opening of the activity 2 times. How can I block the ability to press a button after the first press?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.create) {
// start activity
}
return super.onOptionsItemSelected(item);
}
I know about setEnabled, but when I return to enabled activity, it remains false, and it seems inconvenient to save MenuItem and return its state. Anyone have any ideas how to do this optimally?

I found the best solution to this problem. To do this, add the FLAG_ACTIVITY_CLEAR_TOP flag to the Intent. Thanks to this, it will be impossible to create 2 activities.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Related

What is the advantage of using Intent() instead of finish() when going back to the previous page?

My professor has showed us that the way to go back to the previous activity is to create a new activity. But I noticed that this adds the a new activity onto the stack creating multiples of the activity. When I asked him about it he could not give a clear answer and I was wondering what is the main advantage of creating a new activity and increasing the size of the application stack instead of using finish() just to return to the main activity?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//more code
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_products) {
Intent in = new Intent(this, ProductsActivity.class);
startActivity(in);
}
}//this is how we move from main activity to the products activity
ProductsActivityCode:
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_home) {
Intent in = new Intent(this, MainActivity.class);
startActivity(in);
}
return super.onOptionsItemSelected(item);
}
As you can see we create a new activity in both directions when going to the products activity and coming back to the MainActivity()
My professor has showed us that the way to go back to the previous activity is to create a new activity.
Well, that's wrong because "the way to go back to the previous activity" is to do nothing in code and press the back button since that's default Android behavior.
But I noticed that this adds the a new activity onto the stack creating multiples of the activity.
That is the expected behavior based on the code you posted, yes.
When I asked him about it he could not give a clear answer
Obviously we don't have the full context and just your comment, but if your professor teaching you Android development can't explain how navigating between Activities in Android works, you might want to find a new professor ...
I was wondering what is the main advantage of creating a new activity and increasing the size of the application stack instead of using finish() just to return to the main activity?
There is no advantage and in fact has several disadvantages, as you noted. This is not the way "to go back" in Android.

Settings Back Button Isn't Working Using Bellow Code?

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().

Prevent ActionBar back button to recreate the MainActivity

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

Start an Activity from Item (ActionBar)?

I'm using ActionBarCompat to make an actionbar for devices under API 11.
It works great and was easy to setup, but I'm stuck.
I have some items on the Actionbar, and it looks great.
Some Items are behind the three dots (ifRoom) and some you always can see.
How do I make so when you click on one of these items so it starts an new Activity?
I tried with switch/case and other methods, but didnt work to send from one Activity to another through the Items. I know how to send from button, imagebutton to another Activity, but not from Items.
My main.xml looks like this:
<item
android:id="#+id/add"
android:title="Lägg till"
android:icon="#drawable/new"
android:orderInCategory="1"
budsnabben:showAsAction="always"/>
And the code in MainActivity looks like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.:
Intent intent = new Intent(this, MapActivity.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;
}
}
The problem is on case R.id.... After Id, I dont get my class map or main, its not there.
Thank you.
SOLUTION:
Just want to thank you Gerard.
I created new Strings in strings.xml.
After that, I did change the title in my main.xml to this:
android:title="#string/add"
I did hard-coded that line like before, therefore it didn't work I think:
android:title="#+id/add"
Thank you once more.
Use public boolean onOptionsItemSelected(MenuItem item) { ... } in your activity using the actionbar and create a switch-case matching item.getItemId() with the IDs from the menu layout. After that creating the appropriate intent like you would on a regular button.

"Back" button on Action bar - Android . How to go "back"?

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);
}

Categories