In Activity1 have 1 button ,if user clicks on button it goes to Activity2.here i want to change button flow based on result in activity10,i.e if result is OK in Activity10 then Activity1 renders then user clicks on button it should show activity5(After reopened the app also app should show this result).I tried by using Bundle but when i reopened the app first scenario executing,it can be achieved by using shared-preference by taking a Boolean variable but dont know how,suggest me with piece of code to handle this multiple activities with shared preference?help me
From any place you can do
int prefVal = PreferenceManager.getDefaultSharedPreferences().getInt("myint", defVal);
and
PreferenceManager.getDefaultSharedPreferences().edit().put("myint", 100).commit();
Based on the last stored value you, probably, can figure out what to display.
Related
I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo
i have button in my first activity called Start.
Now when i click on this button it takes 1 to 2 seconds to load the next activity, now at that time the user clicks on the start button multiple times so what happens is that the next activity will open multiple times.
How to overcome this? Is there any way that even if the user clicks on the Start button multiple times open the activity only once.
Your Options:
On click, disable the button and display a ProgressDialog to the user.
Use the Intent flag FLAG_ACTIVITY_SINGLE_TOP to ensure only one activity is maintained on the stack. Documentation
Use the qualifer launchMode=singleInstance in your AndroidManifest.xml so only one instance of your Activity is allowed at a time. Documentation
I would recommend the first, because it can show the user your application is still working even if it takes a few seconds to do the necessary processing to begin your Activity.
You can put the launch mode of your 2nd activity as "Single Instance" in your manifest file.
Don't use anything like a launchMode or Intent flags. They are used for different purposes.
Description here
What you have to do is:
Show a progress dialog to clearly show the user that an
action(calling 2nd Activity) is in progress. This was user will not
try to click the button multiple times
Simply disable the button's click listener after 1st click. This is not
recommended because user might not be able to know whether he/she
clicked the button. Also this is the case where user tends to click
the button multiple times.
Today I was testing my app (wich is a very simple note application) but some strange behaviour occur .
The app consists of a big ListView which displays all the notes and a button to make a new note (activity A).
When I press the button to "Add new note",it takes me to the activity B
(with all the fields like title and text).
When I press "Add" ,it take me back to the activity A and displays ok
the new Note .
The thing is that I started pressing BACK and it began to take me "back in time" where some notes were still not created,until it reached the time they were 0 notes .
Since I am new to Android devel ,is this normal or is it something wrong with my app ?
When you go from one activity to another then you should finish your activity and then start new activity.
Try the below code:
finish();
startActivity(new Intent(CurrentActivity.this,NewActivity.class));
You probably need to implement some sort of refresh method to update your ListView. Depending on how you are persisting the data, you could create a method that retrieves the latest data and then updates the adapter for the ListView. Once you have that implemented, you should call the method in onResume().
I have a form application, in this application the user clicks on a Menu option(M1) to go to a form (Frm1). This is done using Intent.
The form (Frm1) have two buttons, a back button and a submit button. When the user clicks the submit button the form is submitted, when the back button is clicked the user reaches the menu option(done using Intent).
Now, When the user clicks the back button(he goes to M1) with a half filled form, he must be able to continue with the task when he returns back( to Frm1).
My logic was to collect all the values in the form to a bundle and carry it along the way from Frm1 to M1 and vice versa.
Is this a good approach, got any better idea?
Thanks in advance..!
If data entered in the form (Frm1) is used in your Menu Activity (M1), then obviously you should use bundles and send it between Activities.
Else it may be unwanted logic of working with forms data in Menu Activity.
Just imagine, that you will create new wonderful Activity before M1 in your app (dashboard or something similar to it). Now you have to pass your bundle to first activity, because else you will lose form's state, when user close Menu Activity. It's not good, anyway.
So, if you can encapsulate this logic in Form Activity, I recommend you to make it.
You can use SharedPreferences or Singleton storage. If your form's data is strings, numbers and flags - SharedPreferences is easy, good and safe solution.
Using a bundle is exactly the right way to do this, though if the user has pressed the back button, are you sure they want to keep the data they entered? You will have to provide some way to clear the form.
You can use either Bundle or SharedPreferences.
But better to go for SharedPreferences because Bundle is restricted to the session where as shared preferences is not.
Another option would be to save the form data in the SharedPreferences. The difference between saving it in a bundle and this is that the data will persist upon an app shutdown. Saving the data into a bundle will only last during the application's lifecycle
Good day all, First of all, maybe i went about this the wrong way in the first place. Situation is, i have a listview that when an item is clicked, it creates an intent and calls startActivityForResult() to another activity, to be able to edit the entries in the row. and that other activity after editing the required values, returns back to the calling activity, when the user clicks on a save button. The activity then uses the row Id to update the item.My problem now is that, if the user presses the BACK button instead, the application crashes. i have been looking around and see solutions like using Shared Preferences or onSavedInstanceState(), but i don't know where exactly i should be putting the code? Any help as usual, will be greatly appreciated. Thank you.
p.s: when i look at logcat, its gives a NullPointerException for this line in onActivityResult.
Bundle result = data.getExtras();
If you are assuming you will have a result on the parent thread if the user quits the activity via Back then that is most likely the issue. Try testing if result is null before attempting to use it.