1

I am trying to get the total messages value from second Activity's list. I have implemented this. But it always shows the value of 0 or null in case of String. Can anyone tell me how can I get the ArrayList size in my first Activity.

My code is,

In second activity:

 private static List<String> msgList = new ArrayList<String>();//above oncreate method
 setMsgList(getSMS());//call the method
 int count = getMsgList().size();
 msgcount = String.valueOf(count);
 System.out.println("listcount" + msgcount);//it prints the correct list size

And my first Activity is,

String size=InboxActivity.msgcount;
System.out.println("count:::::::::::::"+size);//it prints 0

Can anyone tell me how can I get the list count in first Activity?

hata
  • 11,633
  • 6
  • 46
  • 69
Jolly
  • 457
  • 1
  • 6
  • 11

6 Answers6

0

Make your list static and create a static get method.

Dave Hulse
  • 70
  • 6
0

Make your msgList public and write this code in your first Activity and it will be ok.

SecondActivity.msgList.size(); 

If you still want to leave the msgList as private, you should make eventually a public static method in your SecondActivity and call it from the first activity like:

SecondActivity.method();
Hitman
  • 588
  • 4
  • 10
  • Making the list public and static or making a public static getter should never be done. That would just be bad design. Android provides an easy solution to pass values between Activities and that is using startActivityForResult to start the Acitivity. – Xaver Kapeller Nov 21 '13 at 07:31
  • Can you explain me why do you say is bad design ? It is bad in memory management or why ? – Hitman Nov 21 '13 at 09:32
  • It's not how things are supposed to work in Android, things like this can if you are not careful turn in to a source for memory leaks and similar undesirable problems. Especially with Acitvities. Just think of for example an orientation change. The whole Activity is destroyed and recreated in such a case. But that would not affect static variables therefor it would ignore the lifecycle and can be a source of problems. – Xaver Kapeller Nov 21 '13 at 09:41
0

Some people here have suggested that you should make the list public, but that is not a good idea. If you want to return a value to the previous Activity you can use startActivityForResult.
Try something like this:

// Start the second Activity in the first Activity not with startActivity but with startActivityForResult
Intent secondActivityIntent = new Intent(this, SecondActivity.class);
// The number is used to identify the result later
startActivityForResult(secondActivityIntent, 0); 

In your second Activity:

// You can set a desired Result with setResult.
// You can attach data to the Result with an Intent.
Intent intent = new Intent();
intent.putExtra("tag", someString);
setResult(RESULT_OK, intent); // Use RESULT_CANCELED if something went wrong

Again in your first Activity:

// Implement this method to retrieve the data from the second Activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check if the requestCode is corresponding to your Second Activity.
    if (requestCode == 0) {
        // Check if result is ok
        if (resultCode == RESULT_OK) {
            // Retrieve the value
            String value = data.getStringExtra("tag");
        }
    }
}

You can find more detailed Information about this topic here.

ann
  • 576
  • 1
  • 10
  • 19
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • when i click the message button it will open the activity where i want to show the list count....and at the same page where i have the read message button it will open the list activity...Now my doubt about ur answer is where i get my intent result and where i have send intent to second activity – Jolly Nov 21 '13 at 08:05
  • To better answer your question you have to give me a little more information. As I understand it you have an Activity with two buttons. One opens the Activity with the list and the other opens the Activity where the list count is supposed to be displayed. Is that correct? If yes, what kind of data are you displaying in your list, where do you get it from and how do you save it? – Xaver Kapeller Nov 21 '13 at 08:24
  • no its not working for me...again it retrieves null from second activity – Jolly Nov 21 '13 at 10:05
0


You can achieve this by the following
get the list count in the 2nd activity and store it in the application object and in the 1st activity get from the application object
the following are sample code snippets

public class App extends Application
{
}
ACtivity 1:

protected void onStart() 
{
    mApp = (IVYApplication)getApplicationContext();
      int count= mApp.getListCount();  //Get list count
    super.onStart();
}

acitvity 2:
protected void onStart() 
{
super.start();
    mApp = (IVYApplication)getApplicationContext();
      mApp.setListCount(List.getcount());  
//getting the count of elements in list and assign in the application boject
}
SIVAKUMAR.J
  • 4,258
  • 9
  • 45
  • 80
  • While this would work I would not recommend it as this is a much more complex and potentially problematic solution for something where a proper solution already exists. startActivityForResult can be used to pass values between Activities in a much better way. – Xaver Kapeller Nov 21 '13 at 07:34
0

To get data from second activity to first activity you can use startActivityForResult() method. Try this link it may help you.

Community
  • 1
  • 1
Velmurugan
  • 36
  • 7
0

I believe, the right way for interaction between activities is to pass data in Intents.

One more possible way is SharedPreferences (although I remember on at least one version of Android I had to get the instance using the application context and store it in a static variable).

The 3rd way is a singleton class.

From the MVC viewpoint, an Activity is a Controller, its lifetime is limited by the lifetime of the View (you likely reuse the View classes from the framework). The number you want to pass from one Activity to the other must most likely belong to the Model.

So consider using StartActivityForResult() instead. Or, probably, defining a Model class that outlives Views, Activities and even processes, because an Android process may be killed and restarted at any moment after onPause().

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127