Show Prompt in Spinner when activity is resumed - android

I have a spinner in my activity which is loaded with the database values. First time when the activity is loaded the spinner is selected to show the prompt text. When i go to the next activity and press back button then the spinner is selected to show the first item. But i need to show the prompt text when the activity is resumed.
How to achieve that? I need your help.

Plz post your first activities info (code and xml). This would help us give an accurate answer.
However, if you programmatically set your spinner with :
mSpinner.setSelection(0); // or whatever index you want to revert to
BEFORE you start up your 2nd activity. You should be good to go.

What you have done, to make spinner show the prompt text, do it in:
onResume method (Override it).
Also, if the thing that causes the spinner to show prompt text, is something that is not set yet, and you set it later(somewhere in onCreate, so it wont re-show it again in future),
you can override also onPause, to unset it.
Activity lifecycle is the key!

Related

How do I update Spinners in a MainActivity when clicking either the ToolBar back arrow or phone Back button?

Problem:
How would I refresh Spinner data in a MainActivity through the use of the ToolBar back arrow or phone "back button?"
What I am trying to do (would like to do):
Right now, clicking the toolbar back arrow or back button just takes me back to the MainActiity previous state... which is expected behavior without any activity updates. What I wanted to do was trigger some updates to the Spinners in the MainActivity.
How the app spinners function (High-Level):
In my MainActivity are two Spinners called Topic and Question used to return results on the choices made and the user clicks on the results returned to take them to other activities. In later activities, the list data used in the spinners may have been updated or changed. For example, a Topic may have been added or removed. So, when the user eventually returns to the MainActivity, I would like for that list to be updated.
What I have done:
My thought was I would need to use the toolbar arrow or phone back button OnClick with an Intent. Truthfully, both the arrow and button have to function the same in this respect. Unless someone thought there was a better solution, which I'm open to, this was my solution for updating the spinners in the main activity. I was struggling to find how this may be done with the ToolBar arrow and the Back Button.... and whether that was the right solution. I would like to avoid creating a menu button to do the same if I can use the arrow and back buttons.
Below are 3 images demonstrating how this works. Image 1, the user chose a Question to return a result. Image 2 is the result with the Question edited. In image 3, the Main Activity spinner retains the old list and value and there is no result because the question was changed and there is nothing to return for this question value now.
Question choice and result
Question was edited by user
Old question list value remains - there's no result due to the change

How to pass data from 2nd activity to 1st (Popup) without press back button and close button?

I want to call data of activity using popup. Let me explain in brief... when i click on EditText of a dialog box
when i click on EditText (Template) then it will open activity.
after i select one option and activity will finish and selected option's value will be set in EditText of Template.
Why not use a Button (perhaps with a drawable) instead of an EditText? This makes more sense because the user won't be typing anything into the EditText.
As for passing the data back and forth, just use startActivityForResult().

reset the selected radio button when recreating the activity

my application activity has a radiogroup. It has 2 radio button.
in the xml file, i select the radiobutton1 as default when i start the activity.
Scenario :
select radio button 2
Go to phone settings and change the language.
go back to the application activity.
the activity is created again. But the radiobutton2 is selected.
Actually when i create the activity it should keep radiobutton1 as default selection. But after changing language and coming to my activity it keeps the previous state even though onCreate() is run.
I want to make radiobutton1 selected when i recreate my activity.
Kindly let me know how it can be..where am i doing wrong.
in my onDestroy() i tried radiogroup.clearcheck()
i tried to check the id of radiobutton and then set radiobutton1.checked(true).
i have onClickListener which i use to select the radio buttons.
Somehow the state is not being cleared and i cannot make radiobutton1 as default selection when i create my activity again.
Are you sure that onCreate() is called again? Because it's likely that the activity is not destroyed and thus is not created again. If that's the case, you can move the code that selects the default radio button to onResume(), which is called every time the activity is displayed, even if it is not created.
Otherwise, you should post your code so that we can help you.

What means this button state when my activity starts?

When my activity starts the button starts as below image, seems that it is selected.
button seems to be selected http://kodeinfo.com/wp-content/uploads/2014/03/post4_1.jpg
Button seems to be selected but I don't select it neither on activity nor layout.
I had checked and this is not related to focus. I tried starting the activity with requestFocus() on a button and does not have the same behavior.
Does anyone knows what is that? What is this state for the button? I don't want this behavior in my app, but I'm not being able to understand the issue in my code because I don't know what this state is.

Refreshing an Activity

I created an Activity which allows navigating between pages with a couple of Buttons (Previous and Next). When the user clicks one of the buttons, the Activity (same) needs to be "refreshed". In order to do this, I set up the buttons to make a call to
onCreate(this);
after they set up the other stuff that the activity uses for the paging to work.
And it is working so far, but I'm wondering if there is a better way. Is there?
You should rethink your approach. Having Previous and Next buttons looks like an iphone's NavigationBar View. Remember that in android you have the back button, so a previous button shouldn't be necessary.
If you wish to do it, check Android's Activity Lifecycle. You should update your Activity on the onStart() method and avoid the call to onCreate(this) which doesn't sound good.
The activity displays data related to a certain day. The navigation allows choosing the day which is being displayed. As such, clicking one of the buttons changes the information which is displayed.
Example:
When the Activity first loads, it shows a given day - let's say August 23rd. Then when the user clicks the activity's "Previous" Button, the Activity shows August 22nd.
Then the user clicks the "Next" button and the Activity shows August 23rd again. If the user clicks the "Next" button again, the Activity will show August 24th.
Doesn't it seam a little strange to only have a "Next" (and rely on the physical "Back" button) ?
Thanks.
I fixed this trough refactoring:
Created a new function with the code that was being executed in onCreate (i.e. Layout initialization, database access and information display). The new function is called by the onCreate method (when the Activity gets the "Create" notification) and at anytime that I need to "refresh" the contents of the Activity (i.e. when a button is pressed).
Thanks for your comments.

Categories

Resources