Search code examples
androidandroid-intentparcelableandroid-bundle

How to receive Intent in Activity


Honestly I did somesearch about this before asking. how to receive intent from activity to fragmentactivity I am trying to use viewpager. I am having error here in get argumentsBundle extras = getArguments();

public class SingleViewActivity extends FragmentActivity {
    ImageView   imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);

   ArrayList<Listitem> personArrayList = new ArrayList<Listitem>();
       // Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");//I used to add it staticly

        Bundle extras = getArguments();
        if (extras != null) {
            extras = extras.getParcelableArrayList(ARG_PERSON_LIST);
            personArrayList.add(item);        }
    //    DemoObjectFragment f = DemoObjectFragment.newInstance(personArrayList);

        ViewPager    mViewPager = (ViewPager) findViewById(R.id.pager);
        Log.d("s", "singleview");


        DemoCollectionPagerAdapter mDemoCollectionPagerAdapter =      new DemoCollectionPagerAdapter( getSupportFragmentManager(),personArrayList);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);

Solution

  • You are going to need two Activities. Since you didn't post the Activity that starts the posted Activity, I have made a simple one that contains only a Button that starts the shown Activity.

    Take note of the commented steps. There are six steps total.

    The first Activity gets the ArrayList data, the passes it to the second Activity, which in turn gives in to the DemoCollectionAdapter.

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
    
            Button button = (Button) findViewById(R.id.button);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Step 1: Build an ArrayList to pass to the next Activity
                    ArrayList<Listitem> items = new ArrayList<Listitem>();
                    items.add(new Listitem("1", "http://developer.android.com/assets/images/android_logo.png"));
                    items.add(new Listitem("2", "https://i.sstatic.net/B28Ca.jpg?s=328&g=1"));
    
                    // Step 2: Create an Intent to start the next Activity
                    Intent intent = new Intent(getApplicationContext(), SingleViewActivity.class);
    
                    // Step 3: Put the ArrayList into the Intent
                    intent.putParcelableArrayListExtra(SingleViewActivity.ARG_PERSON_LIST, items);
    
                    // Step 4: Start the next Activity
                    startActivity(intent);
                }
            });
        }
    }
    

    public class SingleViewActivity extends FragmentActivity {
        public static final String ARG_PERSON_LIST = "ARG_PERSON_LIST";
    
        private ArrayList<Listitem> items;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_page_view);
    
            // Step 5: Get the Bundle from the Intent that started this Activity
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                // Step 6: Get the data out of the Bundle
                items = extras.getParcelableArrayList(ARG_PERSON_LIST);
            } else {
                items = new ArrayList<Listitem>();
            }
    
            ViewPager pager = (ViewPager) findViewById(R.id.pager);
            DemoCollectionPagerAdapter adapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), items);
            pager.setAdapter(adapter);
    
        }
    }