Skip to content

Commit 574a349

Browse files
committed
First fragment send messages -> Second fragment receive messages.
1 parent 81e8a7f commit 574a349

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

app/src/main/java/asia/remix/myfragment/FirstFragment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void onViewCreated( @NonNull View view, Bundle savedInstanceState ){
3434
button.setOnClickListener( new View.OnClickListener(){
3535
@Override
3636
public void onClick( View view ){
37-
NavHostFragment.findNavController( FirstFragment.this ).navigate( R.id.action_FirstFragment_to_SecondFragment );
37+
Bundle bundle= new Bundle();
38+
bundle.putString( SecondFragment.KEY, "from 1" );
39+
NavHostFragment.findNavController( FirstFragment.this ).navigate( R.id.action_FirstFragment_to_SecondFragment, bundle );
3840
}
3941
} );
4042
}

app/src/main/java/asia/remix/myfragment/SecondFragment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import android.view.View;
77
import android.view.ViewGroup;
88
import android.widget.Button;
9+
import android.widget.TextView;
910

1011
import androidx.annotation.NonNull;
1112
import androidx.fragment.app.Fragment;
13+
import androidx.fragment.app.FragmentResultListener;
1214
import androidx.navigation.fragment.NavHostFragment;
1315

1416
public class SecondFragment extends Fragment{
1517
static final String TAG= "SecondFragment";
18+
static final String KEY= "SecondFragment";//for recevie key
1619

1720
@Override
1821
public void onCreate( Bundle savedInstanceState ){
@@ -37,6 +40,11 @@ public void onClick( View view ){
3740
NavHostFragment.findNavController( SecondFragment.this ).navigate( R.id.action_SecondFragment_to_FirstFragment );
3841
}
3942
} );
43+
44+
TextView textView = view.findViewById( R.id.textview_second );
45+
Bundle b= ( null == getArguments() ? new Bundle() : getArguments() );
46+
String s= requireActivity().getString( R.string.hello_second_fragment );
47+
textView.setText( String.format( s, b.getString( KEY, "-" ) ) );
4048
}
4149

4250
@Override

0 commit comments

Comments
 (0)