-2

I need the chapterId and lessonId value in Tablayout fragments .

one of the solution to send data from activity to fragment is sending value with public method in activity and create a new instance in fragment and give the value in fragments .

I have This activity where i get bundle in that .

 public class DetailActivity extends AppCompatActivity {

        private String chapterId;
        private String lessonId;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);

            getBundle();

        }

        private void getBundle() {
            Bundle bundle = getIntent().getExtras();

            if (bundle != null) {
                chapterId = bundle.getString("CHAPTERID");
                lessonId = bundle.getString("LESSONID");
            }
        }

      ========  Method for sending data to other Activities and Fragments=====
        public String  getId(){

        }

    }

Now my question is how can i write this method . Thanks

kiana rahimi
  • 246
  • 4
  • 16

2 Answers2

0

for activity:

Intent intent = new Intent(this,yourActivity.class);
intent.putExtra("bundle",bundle);
startActivity(intent)

in activity :

Bundle extras = getIntent().getExtras();
 if (extras != null) {
            chapterId = bundle.getString("CHAPTERID");

        }

for fragment

Fragment myFragment = new MyFragment();
myFragment.setArguments( bundle );

in fragment

Bundle extras = getArguments();

if (extras != null) {
            chapterId = bundle.getString("CHAPTERID");

        }
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
0

You can do this type

Declare Two Methods

public String getChapterId()
{
    return chapterId;
}

and second methods

 public String getLessionId()
{
    return lessonId;
}
Dixit Panchal
  • 3,406
  • 1
  • 11
  • 14