Mark Hackenberg
Mark Hackenberg

Reputation: 68

Passing Data From an Activity to a Fragment in Android

I am very new to Java and Android programming. I am trying to figure out how to pass a bundle of variables from one activity through another Activity that uses setContentView to an xml file that is attached to a class that extends ListFragment. Basically, I want the data from the 1st activity to be able to be accessed by the Fragment that is accessed in the 2nd activity. I've been trying to research this to avail so far so please forgive me if the answer exists on here already.

I have an activity called Login.java that does the following:

Intent intent = new Intent(this, ListActivity.class);
Bundle varBundle = new Bundle();
varBundle.putString("companyKey",companyKey);
varBundle.putString("techID",techID);
varBundle.putString("userID",userID);       
varBundle.putString("startDate",startDate);
intent.putExtras(varBundle);
startActivity(intent);

In ListActivity.java, I have this so far:

public class ListActivity extends Activity 
{
public String companyKey;
public String techID;
public String userID;
public String startDate;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Bundle vars = getIntent().getExtras();

    companyKey = vars.getString("companyKey");
    techID = vars.getString("techID");
    userID = vars.getString("userID");
    startDate = vars.getString("startDate");

    setContentView(R.layout.fragmentlist);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list, menu);
    return true;
}

}

I set the content view to fragmentlist.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titles"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.technicianappointmentlist.OrderList" />

Fragmentlist.xml is tied to OrderList.java. This is where I want the data from the bundle passed into ListActivity.java to be able to be accessed.

public class OrderList extends ListFragment
{
//THIS IS WHERE I WANT TO ACCESS THE VARIABLES PASSED INTO ListActivity.java    
public OrderList() 
{
    super();
    // TODO Auto-generated constructor stub
}


@SuppressWarnings({ })
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}
}

Is this possible? Again I apologize if this is a rookie question but I haven't found the answer to this yet.

Thank you in advance.

Upvotes: 2

Views: 2517

Answers (2)

Karakuri
Karakuri

Reputation: 38585

If your fragment is always only going to be attached to that Activity, you can make a public method on your Activity. Then just enforce that relationship between the fragment and the activity:

public class OrderList extends ListFragment {
    private MyListActivity activity;

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof MyListActivity)) {
            throw new IllegalStateException("must be attached to an instance of MyListActivity!");
        }
        this.activity = activity;
    }

    /* elsewhere you can call public methods defined in MyListActivity */
}

If this coupling is too tight for your taste, you can make an interface instead, and have the activity implement that interface.

You can do things the other way as well. Find the fragment from the activity using the FragmentManager, check if it's an instance of OrderList, and call public methods on it:

OrderList fragment = (OrderList) getFragmentManager().findFragmentById(R.id.titles);
fragment.whatever();

Upvotes: 1

Neoh
Neoh

Reputation: 16164

You can call (from within your fragment)

String companyKey = ((ListActivity)getActivity()).companyKey;
String techId = ((ListActivity)getActivity()).techId;
String userId = ((ListActivity)getActivity()).userId;
String startDate = ((ListActivity)getActivity()).startDate;

Upvotes: 1

Related Questions