0

Overview::

  • I am receiving data from another activity to my present activity as intents and im storing it in present activity as a variable named recievedData
  • Now my present activity has a fragment in it.
  • The fragment has a textview

Q::So how can i set the textview ?


MainActivityTwo.java

public class MainActivityTwo extends FragmentActivity
{

    FragmentManager objFrgMng=getSupportFragmentManager();
    FragmentTransaction objFrgTrns;
    FragmentTwo objFrgTwo=new FragmentTwo();
    String recievedData;
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_act_two_container);        
        Bundle extras=getIntent().getExtras();
        recievedData=extras.getString("key");   
        Log.d("RECIEVED-DATA", recievedData);
                getSupportFragmentManager().beginTransaction().add(R.id.mainActTwoContainerId,objFrgTwo, "MainActFrgTwoTag").commit();


    }

}

main_act_two_container.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/mainActTwoContainerId" >


</RelativeLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#084B8A" >

    <TextView
        android:id="@+id/textViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="161dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

FragmentTwo.java

public class FragmentTwo extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View ObjFrgOneView=inflater.inflate(R.layout.fragment_two, container, false);

        return ObjFrgOneView;

    }

}

note:: all im doing is dynamically

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • The correct way would be: http://developer.android.com/training/basics/fragments/communicating.html – Skynet Jan 14 '14 at 09:53
  • 1
    http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android. check this – Raghunandan Jan 14 '14 at 09:54
  • @Raghunandan..... Thanks i was able to resolve this issue ... i have shared the solution..so it might be helpful to someone – Devrath Jan 14 '14 at 10:07

1 Answers1

0

With the help of Raghunandan in one of comments i was able to resolve this from this link, i am sharing the complete solution in case it might be helpful to someone


MainActivityTwo.java

public class MainActivityTwo extends FragmentActivity
{

    FragmentManager objFrgMng=getSupportFragmentManager();
    FragmentTransaction objFrgTrns;
    FragmentTwo objFrgTwo=new FragmentTwo();
    String recievedData;
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_act_two_container);

        Bundle extras=getIntent().getExtras();
        recievedData=extras.getString("key");

        Log.d("RECIEVED-DATA", recievedData);

        //sending data to second fragment in second activity
        Bundle bundle = new Bundle();
        bundle.putString("edttext", recievedData);
        objFrgTwo.setArguments(bundle);


        getSupportFragmentManager().beginTransaction().add(R.id.mainActTwoContainerId,objFrgTwo, "MainActFrgTwoTag").commit();


    }

}

FragmentTwo.java

public class FragmentTwo extends Fragment{

    TextView tv;
    String strtext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View ObjFrgOneView=inflater.inflate(R.layout.fragment_two, container, false);

        strtext = getArguments().getString("edttext");    


        return ObjFrgOneView;

    }

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

        tv=(TextView) getActivity().findViewById(R.id.textViewId);
        tv.setText(strtext);
    }

}
Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297