reset the selected radio button when recreating the activity - android

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.

Related

Instructions Activity Android Studio

I would like to create activity in which it would be explained how the app is supposed to be used. On a click on a button, I would like an Activity to be opened which has next and previous buttons on the bottom. So the first screen after the click on a button would show the first instruction and you can press next to see the next instruction which opens a new screen. After going through all the instructions, I would like a Finish button to return back to the main activity.
My question is can this be made in a single activity or is it actually more activities that differ only by the text and pictures they contain?
Yes it can be done in only one activity(actually 2 if you include the main activity).
What you can do is have multiple TextViews, and when the next button is pressed, you can toggle that 1st instruction's visibility
. And so on for the next instructions. The Next/Previous buttons should determine what Textview is currently visible, so they can keep track what Textview to show next. Use switch statement

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.

Show Prompt in Spinner when activity is resumed

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!

how to resolve radio button problem when we changing the orientation?

hi all how to implement code for Radio button when i take the radio group i take only two radio buttons from that radio button when i select the second radio button after changing the orientation second radio button is disable (i mean when i select the second radio button in the portrait mode if i change to landscape mode radio button first one is enable and second one disable but my intention is second have to be enable )how to solve this problem so plz help me over come from this problem
Simply add android:configChanges="orientation" in AndroidManifest.xml of that Activity.
When changing orientation on Android, the activity is reinitiated, i.e. it is destroyed and then restarted. What you need to do is to save your current state in the OnDestroy method and read it in the OnCreate method.
Read more on the android development webpage on activity lifecycles

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