How can i update an activity in android studio - android

I've created an app with two activities. On the second activity i can create new buttons by pressing a floating action bar button inside the activity; but the problems is when i return to the main activity and come back all the added buttons are gone.
I was wondering how can i save these added views so whenever i exit that particular activity or the app entirely, views won't perish.
I know i can save the views' values by "Preferences" commands but that only can save the views' parameters not themselves and if i go that way i have to recreate each view again each time i enter the activity.
if i could update the activity whenever views are added to it that would be excellent.
I would appreciate any help.

You can save information about views in preference like number of buttons created. for example you clicked button 4 times then save numOfButtons value as 4. In onCreate method you can check values of numOfButtons and execute that block of code 4 times to generate buttons.

Related

What is the best practice to use multiple layouts in one Activity or Fragment?

I have a fragment for user registration in that fragment's XML, I have a fixed header and a footer. The header consists of steps for showing the current progress or number of the visible fragment. The footer consist of two buttons for next and back, when I click on next button I want to show the second layout of the registration form and change the colour of the header step so that user can check that he has completed one step and is on the second step, and back button appears because I'm on the second layout. When the user finishes entering all the fields I want to check for validations like email patters is correct, password pattern is correct etc, then send all that form data to the server for user registration. My Question is what is the best practice to do such thing, should I use one fragment and in that fragment I include all the layouts and play with the visibility of the layouts, like if i press the next button the second layout gets visible and first gets gone but the data filled by the user must not get lost because i want that data to get validated and send to server. Or i should make a MainActivity in that main activity i call all the three fragments on button click like if I press the next button the second fragment replaces the first. But again the question is if I go with the activity containing multiple fragments method what will happen to the data of the fragments when user will click on next and back button?
I know it's a long summary of my question but I want to know what's the best practice to do in such cases.
Any help would be appreciated.
You should use fragments within a viewpager. This will also additionally give you the dots at the bottom visualizing what page and how many pages in total.
read more about viewpager

Android: How to handle a lot of UI elements change depending on SharedPreferences values

Introduction:
I have created custom settings for my application and all of setting's data is depending on SharedPreferences.
Basically I have some switches (7 to be exact) and spinners, and textviews and on activity launch I get value from SharedPreferences which can be booleans, int or strings.
I have created complex logics and I don't want to switch to Preference activity.
Previously:
I had an activity with viewpager and TabLayout and activity was launching with a long delay.
Basically what used to happen was: user clicked on button to launch settings and he had to wait for minimum 3 seconds (sometime even 5 seconds) with MainActivity freezed and then Settings would launch.
Currently:
So I transformed fragments to activities and tried to use NavigationDrawer to switch between 3 activities instead of 3 tabs.
I created a base activity with NavigationDrawer and extended it from other 3 activities so all of them would use same NavigationDrawer.
Basically now on button click I'm launching first Activity and it gets launched immediately but still there's a delay of 2 seconds and it keeps showing white screen and then would load content with switches, textviews and spinner values set.
I tried with Fragments but they lag when replacing one with another and also loading time was same.
How settings works:
One spinner is depending on other spinner's state (enabled/disabled, on/off). When activity is created I have to get values from SharedPreferences like for instance a boolean with value true and turn on first switch which will enable all other switches and should update some shared preference values.
All this happens in activity's onCreate so delay is justified obviously.
My questions:
Am I doing it right or should I use some background tasks?
How should I reduce the loading time (if possible)?
Is there another way to get these values elsewhere maybe and then when activity is launched I recall these values and content is shown immediately?
Edit: Unfortunately I don't have demonstrative code. I tried to explain all in my post to give more or less an idea of what I did.

How to use the same layout but change the text after clicking a button in a previous activity

im new to programming.. i'm building an app with a about 18 buttons.. after clicking the button it takes me to a new activity..
the new activity layout is the same for all the buttons but i want to change the text (title and text).
e.g. activity1 is a list of buttons of football clubs (Barcelona, Madrid,...etc). once clicked it will take you to activity 2 which displays the information about the club (text).
Must i create an activity for each of all 18 buttons? (imagine if each button has a few more buttons and that will multiply to over 50 activities and all the classes asociated with them)
Is there a way i can code my target page to use the same layout but just pull different stings of text which corresponds to the button?
Appreciate all the help.. thanks allot!
It sounds like you want to change only text.so you can keep track of which button is clicked in your previous activity.
You can store that value you are using to keep track which button clicked into
1)SharedPreference and retrive into another activity link for how to use it.
2)Intents -Store value into intent and pass to another activity and retrive it.(You can use bundle too here) link for how to use it
After retriving value you can use setText("") function hope it will help you
Just start the same Activity again and keep some kind of index either in the Application class, a Database or SharedPreference use this index to keep track of what you should be displaying in each instance of the same Activity.
You can start the same Activity as many times as you want and it will still be treated as a separate one and the back stack will still function the same.
Use same Activity and same layout and pass some indicator to activity or pass title and text from current activity to next activity and display title and text accordingly

Android Two layout for one activity

I have a form, and I have chosen to divide it in two steps.
To do it, I created two layouts for a same activity. When the user complete the first form, I call the second layout with :
setContentView(R.layout.activity_form2);
The problem is, if the user wants to come back at the first step of the form, it's not running, because he comes back to the previous activity.
Is it correct to do that, or I need to use fragment?
Otherwise, how can I do to come back on the previous layout, and not the previous activity?
Never set different layout's for the same Activity. You can navigate to a different Activity or you could use Fragments.
Layout is set to the Activity and when you click back button Activity is popped from the back stack and previous Activity in the stack takes focus. So setting different layouts for the same Activity is not a good choice.

run time layout manipulation

I have a grid type of layout. When the app first loads, there are 3 buttons. I am using "Adapter" for the layout.
ONCLICK of the button, I want to refresh the same activity but with different set of 9 buttons.
Do I start a new Activity in all? OR Make a temporary activity to start the previous activity (and how)?
Since the ONCLICK event is written in the "Adapter" part of the code, starting new activity on click of the button is difficult. (is out of my knowledge).
if you are using adapter i.e., like baseadapter then you can try:
adapter.notifyDataSetChanged();
directly without starting activity again.
If you want the user to go back to the 3 button view on click of Back button, it will be easier to have the 9 buttons in a different Activity.
Otherwise, you can have the 3 buttons and 9 buttons within two different LinearLayouts in the same activity and hide the second layout using setVisibility(LinearLayout.GONE);
On Click of the button, you can hide the first layout and enable the second one using setVisibility(LinearLayout.VISIBLE);
In the adapter class, we can start an activity using context.startActivity(intent) I did not know that we can access "start Activity" from adapter...
but now it's working just fine!!
Thanks a lot for your recommendation...

Categories

Resources