Open Activity A and load a specific piece of Activity A FROM Activity B

I have an Activity A that loads fragments based on a menu selection. If I make an intention from Activity B to Activity A, the default fragment of Activity A will load. I want to achieve loading another fragment from Activity A instead of the standard one. Hope this at least makes sense. I tried to do it β†’

In action B, I have β†’

btnlinkToForum.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ReaderActivity.class); i.putExtra("fragmentNumber", 1); startActivity(i); } }); 

And I'm trying to use putExtra data to load a specific fragment in Activity A, like this -> I put this code in the onCreate method

 if (getIntent().getIntExtra("fragmentNumber", 0) == 1) { FragmentManager fm = ReaderActivity.this.getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); FragmentForum fragment = new FragmentForum(); if (fragment != null) { // Replace current fragment by this new one ft.replace(R.id.activity_main_content_fragment, fragment); ft.commit(); // Set title tvTitle.setText("Forum"); } } 

This, however, loads the default snippet ... Any help would be greatly appreciated

+7
android android-intent fragment
source share
2 answers

Can someone give an example of how this can be done ... Whenever I try to get a value using, for example ,. GetIntent () getStringExtra ("all"); I get a null value ... -

You should definitely get values ​​from one action to another, for example:

Activity_A.java

 Intent i = new Intent(this, SignUpActivity.class); i.putStringExtra("key", value); startActivity(i); 

Activity_B.java

 onCreate() { Intent i = getIntent(); String var = i.getStringExtra("key"); .. } 

PS: If you want to pass an object

 i.putSerializable("object", object); 

and

 (Class) i.getSerializable("object"); 
+3
source share

This works;) My problem was that I mixed the identifiers of the buttons that initialized the intent, so basically I, although I pressed the right button because the button name was correct, but I referenced the wrong button identifier, so I always got zero value! Below is the code that works for me! In activity B:

 btnToForum.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(RssListActivity.this, ReaderActivity.class); //pass data to an intent to load a specific fragment of reader activity String fragmnet = "forum"; i.putExtra("fragment", fragmnet); startActivity(i); } }); 

In action A:

  Intent i = getIntent(); String fragmentName = i.getStringExtra("fragment"); String forum = "forum"; Log.e("Test1", "Test1" + fragmentName); if (fragmentName != null && fragmentName.equals(forum)) { Do some magic here ;) } 
+6
source share

All Articles