Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass fragments between activities

I want to make an application that can support portrait and landscape. The layout has two panes, on the left is the options and the right shows the result. When an option is selected the right pane shows it. But for portrait there is not enough room, so a separate activity is needed. Each option produces a different type of fragment, so I don't want to make an activity for each option when all that changes between activities is what fragment is being added there. I want to pass a fragment from the main activity to the new one, how would I do this?

like image 465
Hank Avatar asked Jul 11 '11 20:07

Hank


People also ask

How do you pass data between fragments and activities?

To actually pass the data between fragments, we need to create a ViewModel object with an activity scope of both the fragments, initialize the ViewModel , and set the value of the LiveData object.

How do you pass data between two activities?

For this, Intent will start and the following methods will run: putExtra() method is used for sending the data, data in key-value pair key is variable name and value can be Int, String, Float, etc. getStringExtra() method is for getting the data(key) that is sent by the above method.

Can you pass the data between two fragments?

Share data between fragments Additionally, both fragments must handle the scenario where the other fragment is not yet created or visible. These fragments can share a ViewModel using their activity scope to handle this communication.

What is difference between activities and fragments?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.


1 Answers

EDIT: Moved what asker actually wants to top.

If you want to pass data to an Activity when creating it, call a version of Intent.putExtra() on the intent that is used in startActivity(). You can then use getIntent().getStringExtra() to (for example) get a string extra in the activity.

Say you have a piece of string data in your first activity called myString.

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(EXTRA_NAME_CONSTANT, myString);
startActivity(intent);

Now in your new activity in onCreate you would do:

String myString = this.getIntent()
        .getStringExtra(EXTRA_NAME_CONSTANT, "default return value here");

A few notes:

  • For that EXTRA_NAME_CONSTANT, I do mean to make a string constant of the form "your.package.name.SomeString" for example "com.example.MyString". Personally I'd even use a resource (accessed in the form getString(R.string.extra_my_string)) for the extra's name. They recommend you prefix it with your package name.
  • You can put and get many types of data from strings to arrays to even serializable data.

Instead of making a separete activity for different layout orientations consider using resource qualifiers to provide alternative layouts.

To summarize, make two layouts in a structure like so:

/res/layout/yourlayout.xml
/res/layout-land/yourlayout.xml

Where both XML files are named the same. Then make your default portrait layout in one and a landscape version in the other.

When you inflate the layout in onCreate (and when it does so automatically on a layout change during runtime) it will select the correct layout for you.

like image 173
Ribose Avatar answered Sep 22 '22 03:09

Ribose