0

I want to receive intent on the two activity classes, but not sure how since startActivity can only start one intent to only one activity class

private void populateListView()
{
    Log.d(TAG, "Populating listview");
    Cursor data = dbManager.getTitle();
    final ArrayList<String> listTitle = new ArrayList<>();
    while (data.moveToNext())
    {
        listTitle.add(data.getString(1));
    }
    final ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listTitle);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
        {
            String name = adapterView.getItemAtPosition(position).toString();

            Cursor data = dbManager.getTitleID(name);

            int titleID = -1;
            while (data.moveToNext())
            {
                titleID = data.getInt(0);
            }
            if (titleID > -1)
            {
                Intent intent= new Intent(List_Title_Activity.this, MainActivity.class);
                //Intent intent2= new Intent(List_Title_Activity.this, ViewListContents.class);
                intent.putExtra("id", titleID);
                intent.putExtra("title", name);
                startActivity(edit);
            }
            else
            {
                Toast.makeText(List_Title_Activity.this, "No id associated with that name", Toast.LENGTH_SHORT).show();
            }

        }
    });

MainActivity and ViewListContent:

Intent receiveIntent = getIntent();
selectedID = receiveIntent.getIntExtra("id",-1);
Max
  • 739
  • 2
  • 8
  • 23
Dhwng7
  • 57
  • 4
  • I don't think there is a predefined way of doing this thing. You can do this by some workaround but this must be not a good practice and make an app error-prone. You can post the feature you are trying to build and there must another way to produce it. – Akshay Nandwana Mar 20 '20 at 07:50
  • _"how can i receive intent on 2 activities?"_ You can't. Re-think your approach to the problem that you are having. – Markus Kauppinen Mar 20 '20 at 08:50

2 Answers2

0

1 Activity is 1 page in android. That is, you can call/view 1 page/Activity at a time. If you want to share same data to multiple pages in use shared preference / Database. Or if you are using 1 activity and multiple fragments in it, use interface.

Jinn
  • 72
  • 7
0

if you want to send data from one activity to 2nd activity you can use simple intent and pass data from first activity to second . But if you want to send the data first activity to second and get data from second activity to first activity i suggest you to use startActivityForResult. for more see here

Amit pandey
  • 1,149
  • 1
  • 4
  • 15