Featured Posts

{getPosts} $label={seo} $style={1}

How to pass data from activity to fragment in android studio


data from activity to fragment

  • If you want to pass data from activity to fragment in android then first you have to create an activity named MainActivity and paste the following code into the MainActivity.java file.
package com.techevolver.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    Button send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager=getSupportFragmentManager();
       final FragmentTransaction transaction=manager.beginTransaction();
       final DataFragment fragment=new DataFragment();
       send=findViewById(R.id.send);
       send.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Bundle data=new Bundle();
               data.putString("Subject","Java");
               fragment.setArguments(data);
               transaction.add(R.id.Result,fragment);
               transaction.commit();
           }
       });
    }
}
  
  

  • Paste the following code into main_activity.xml file under resàlayoutàmain_activity.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/send"
        android:text="Send"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Result"></FrameLayout>
</LinearLayout>


  • Now you have to create a fragment in the android studio project. For that go to the project tab on your left side and right-click on the app tab and go to NewàFragmentàEmpty (Blank) Fragment.
Type name of fragment named Data Fragment and press enter.  A new fragment will be created into your android project.
Now open fragment_data.xml file and paste the following code:

<?xml
version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".DataFragment">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>



  • Now set the function into DataFragment.java file. Open DataFragment.java and paste code:

package com.example.test;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DataFragment extends Fragment {
    TextView text1;
    public DataFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_data, container, false);
        text1=v.findViewById(R.id.text);
        Bundle b=getArguments();
        String data=b.getString("Subject");
        text1.setText(data);
        return v;
    }
}


  • Note that if your project support AndroidX file then you have to add the following dependencies:

 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'androidx.appcompat:appcompat:1.0.2'
 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
 implementation 'androidx.legacy:legacy-support-v4:1.0.0'
 testImplementation 'junit:junit:4.12'
 androidTestImplementation 'androidx.test:runner:1.1.1'
 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 


Post a Comment

Thanks for suggestion

Previous Post Next Post

Contact Form