Wednesday, 19 August 2015

Passing Value From an Activity To fragment

For Passing value from activity to activity but for passing value from an Activity to fragment or from fragment to fragment we use bundle,
I will show to how to use Bundle for this.

Step 1:
 Create Simple Project with a Single Activity.

Paste Following Code into your activity_main.xml;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivityFragment">


    <FrameLayout        android:id="@+id/mainframelayout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentTop="true">

    </FrameLayout>
</RelativeLayout>



Step 2:
Paste Following Code into your MainActivity:
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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


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


    public void Replace_Fragment() {
        // Create fragment and give it an argument specifying the article it should show        MainActivityFragment myFragment = new MainActivityFragment();
//This Portion is used for Setting values to bundle 
        Bundle args = new Bundle();
        args.putString("Name", "Wellcome From Activty To Fragment");
        myFragment.setArguments(args);
//Till Here
android.support.v4.app.FragmentTransaction transaction_two = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction_two.replace(R.id.mainframelayout, myFragment); // Commit the transaction transaction_two.commit(); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onBackPressed() { int count = getSupportFragmentManager().getBackStackEntryCount(); //For Using Back button if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStack(); } else { super.onBackPressed(); } } }

Step 3:
Create a Java Class which extends fragment: and Paste Following Code in it.
package com.example.eczpk0062.passingvaluestofragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/** * A placeholder fragment containing a simple view. */public class MainActivityFragment extends Fragment {

    TextView txt4;

    public MainActivityFragment() {
    }

    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        Bundle mybundle = getArguments();
        String title = mybundle.getString("Name");
        txt4 = (TextView) rootView.findViewById(R.id.textView4);
        txt4.setText(title);


        return rootView;
    }


}
Step 4:
Paste Following Code in your xml layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivityFragment">

  
    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Enter Father Name"        android:id="@+id/textView4"        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />

</RelativeLayout>





No comments:

Post a Comment