0

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.

Nikhil Lotke
  • 605
  • 7
  • 15
Moz
  • 109
  • 2
  • 10
  • You are confused using `AsyncTask`. See this link https://stackoverflow.com/questions/14250989/how-to-use-asynctask-correctly-in-android – Faizan Mubasher Jan 02 '18 at 13:11

2 Answers2

0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Mayank Garg
  • 1,284
  • 1
  • 11
  • 23
  • thanks!! it makes a lot of sense to me now ( that's the exact solution to my problem, now I'm out of confusion thanks!!! ). I'm already using SharedPrefences from class A to store the datas gotten from my firebase database & when class B gets opened, it gets the class A SharedPrefences and update the class B views that needs to be updated with it's required data, still I added real-time checking in class B so class A won't only be the one responsible for updating the SharedPrefences. ( thanks!!!! ) – Moz Jan 02 '18 at 17:03
  • please upvote(click on top button) it that would help me to increase my reputation – Mayank Garg Jan 02 '18 at 17:43
  • I've tried it already but Stackoverflow said I need the minimum of 10 reputation to upvote the answer – Moz Jan 03 '18 at 08:37
  • Thanks to support me. – Mayank Garg Jan 03 '18 at 14:04
0

You can do two things,

  1. 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.

  2. You can write it Sqlite Database and retrieve it back from 2nd Activity.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Chethan Shetty
  • 179
  • 1
  • 10