Poke
Poke

Reputation: 45

getIntent() doesn't retrieve previous activity's intent and return null

I am trying to post in an ArrayList of strings from one activity to another when the option menu is pressed. However, when I try to getIntent() in the next page, it returns null value.

Option menu when item is selected code:

public boolean onOptionsItemSelected(MenuItem item) {
  if(item.getItemId()==2){
       ArrayList<String>data1=new ArrayList<>();
       data1.add("Hello");
       data1.add("bye");
       Intent i = new Intent(MainActivity.this,SecondActivity.class);
        i.putExtra("dataList",data1);
       startActivity(i);
   }
    return true;
}

Retrieve Data Activity :

public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
   lListView = (ListView)findViewById(R.id.lListView);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
    lListView.setAdapter(adapter);
    }
}

Upvotes: 1

Views: 884

Answers (6)

SinghIsBling
SinghIsBling

Reputation: 57

Try in the other Activity in onCreate():

ArrayList<String> data = (ArrayList<String>) getIntent().getSerializableExtra("dataList");

or

ArrayList<String> data = (ArrayList<String>) getIntent().getParcelableExtra("dataList");

Upvotes: 0

YuDroid
YuDroid

Reputation: 1639

You should use i.putStringArrayListExtra("dataList", data1); to set the ArrayList in your code

Upvotes: 0

koutuk
koutuk

Reputation: 832

Check changes i have made in your code.. getIntent() should be called inside oncreate...

public class SecondActivity extends AppCompatActivity {
    ListView lListView;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    //changes
       Intent intent = getIntent();
       ArrayList<String> data =            
       intent.getExtras().getStringArrayListExtra("dataList");
       //changes
       lListView = (ListView)findViewById(R.id.lListView);
      adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        lListView.setAdapter(adapter);
        }
    }

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

At first set

 i.putStringArrayListExtra("dataList",data1);

Then call in Oncreate() section ;

 Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");

Finally

public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Intent intent = getIntent();
    ArrayList<String> data = intent.getStringArrayListExtra("dataList");

    lListView = (ListView)findViewById(R.id.lListView);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
    lListView.setAdapter(adapter);
    }
}

Upvotes: 0

Yury Fedorov
Yury Fedorov

Reputation: 14928

Move this code into your onCreate method:

Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");

Intent does not exist at the point that you try to get it, it is being passed later in your activity's lifecycle. That is why you are getting null.

Upvotes: 1

Pavya
Pavya

Reputation: 6025

Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");

add this lines in oncreate

Upvotes: 1

Related Questions