How to retrieve data from background in Android App - java

There is 1 Button on my MainActivity and when user clicks on it, it starts the UserProfileActivity to view the current signed in user profile details along with names (values) on TextView.
e.g. it's something like this >>
"Name: Bean Smith",
" Age: 27 ",
" Gender: Male ".
I've already created the layout for the same and I know how to retrieve data's from the DatabaseReference to display values on TextView acting as the user details (from DataSnapShot). Issue:I want to load the user details before my MainActivity will start, so that when the user clicks on the button to check the user profile, the values should get auto populate automatically.
I've searched and found AsyncTask but I Don't really know how or when to use this. please any help from you guys will be highly appreciated..
thanks.

Suppose you have two screens, A and B, or you want that when screen B opens, it starts with filled data, use the following logic.
Firstly, when you are going from one activity to another, that time load all data on screen A and pass the data to screen B by intent, and get or check the received intent values using this, you can achieve your destination:
In class A
Intent intent=new Intent(A.this,B.class);
intent.putExtra("data","data_value");
startActivity(intent);
In class B
if(getIntent().getExtras()!=null)
{
String a=getIntent().getStringExtra("data").toString();
}
as follow you can send multiple data over the two screens.

You can do two things,
As Mayank Garg said you can download the data using AsyncTask task in MainActivity and pass them to other activity but in this case u have to be careful about the null checks.
You can write it Sqlite Database and retrieve it back from 2nd Activity.

Related

How to change an object's fields from an activity it was not initialized in

This is my first time building a complex android app so I apologize if this is a vague question (please ask me to expand!) but I'm a little lost on how I should design/implement my application.
To make it easier, say I have a list of cars to choose from in the main activity.
Once a car is selected, it will take me to its specific settings page in another activity (the master/detail flow activity).
What I'm thinking of doing is the following:
Create a class for car with all of its fields as the various settings
Create a list of them in MainActivity
Once a car is selected, pass that specific car to the master/detail flow activity using intent
From here I'm a little confused, but I think that in the 2nd activity depending on the settings the user selects, the car in the list in the MainActivity will have its fields changed even though it was changed in another activity?
Is this the best way to approach changing settings for a list of objects initialized in the MainActivity, from another activity? I'm mostly confused on if I'm allowed to do that 4th point.
You will need to return the object to the first activity and update your list with the new data.
Start the detail activity with the startActivityForResult and then in your second activity after doing the modifications:
Intent returnIntent = new Intent();
returnIntent.putSerializableExtra("result", theObject);
setResult(Activity.RESULT_OK,returnIntent);
finish();
And then, in the first activity use the onActivityResult method to retrieve the modified object.

How to populate same layout xml several times?

After this when the user click soba i want to show an image And description on how to make it the recipe in different layout.But after that if they click soup
i want use the same layout file to different image and description.
Can i use same layout xml file to do so OR make different layout xlm for all, if so how do i do it.
And how to set Onclicklistener on ListView.
I'm using custom ArrayAdapter<food>.
I am new to android and this is my first app so any help would be great .
Thank you in advance .
Let's say you have 2 activities, ListActivity which looks like in your posted picture, and DetailActivity that shows the recipe detail and how to cook it. Then you can do it like this:
Add OnItemClickListener to your listview.
Make sure you chose the information to you want to send to the DetailActivity so it will know which recipe is clicked (eg: the recipe ID).
Inside the OnItemClicked method, create an Intent object, then put the information to that intent via Intent.putExtra() method.
Inside the OnCreate method in DetailActivity, retrieve the intent extra data, and validate it do determine what to show based on the clicked recipe.

Create dynamic android activities

I am trying to create an android app that has multiple "pages" ,
for example : you can choose any city in the world (e.g : Rome,Berlin,New york..), and by click on it you move to the specific city activity and get specific information, such as: country, nubmer of people.. etc (All the categories are the same, and the information changes for each city )
the specific information should be stored on mySQL database
I would like to ask - how can i implement this on an android application?
thanks!
You have to open a new Activity (CityActivity) adding the selected city to the Intent:
Intent cityIntent = new Intent(this, CityActivity.class);
cityIntent.putExtra("city", selectedCity);
startActivity(cityIntent);
Then in City activity you have to get the value that you sent via Intent:
String city = getIntent().getStringExtra("city");
Now you are able to get the information of the corresponding city and draw the information in a common view.
Use Fragment instead. just create a dynamic fragment that runs on one activity. its can handle almost anything an activity can handle.
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Click Here on how to create Fragment

how to persist data in edittext when entering the activity through menu option of other activity

I have an activity which gets the intent data(string) frm other activity. I want to persist this data and the data(integer) entered by the user in the edittexts. I want to persist all these data when the user enters this activity from the menu option of next activity.
1. You can use SharedPreference.
2. You can also use a file, even a Database. SQLite ships with Android.

Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.

Categories

Resources