Skip to content

Commit 074a634

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

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
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;
1213
import androidx.navigation.fragment.NavHostFragment;
1314

1415
public class FirstFragment extends Fragment{
1516
static final String TAG= "FirstFragment";
17+
static final String KEY= "FirstFragment";//for recevie key
1618

1719
@Override
1820
public void onCreate( Bundle savedInstanceState ){
@@ -39,6 +41,11 @@ public void onClick( View view ){
3941
NavHostFragment.findNavController( FirstFragment.this ).navigate( R.id.action_FirstFragment_to_SecondFragment, bundle );
4042
}
4143
} );
44+
45+
TextView textView = view.findViewById( R.id.textview_first );
46+
Bundle b= ( null == getArguments() ? new Bundle() : getArguments() );
47+
String s= requireActivity().getString( R.string.hello_first_fragment );
48+
textView.setText( String.format( s, b.getString( KEY, "-" ) ) );
4249
}
4350

4451
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public void onViewCreated( @NonNull View view, Bundle savedInstanceState ){
3737
button.setOnClickListener( new View.OnClickListener(){
3838
@Override
3939
public void onClick( View view ){
40-
NavHostFragment.findNavController( SecondFragment.this ).navigate( R.id.action_SecondFragment_to_FirstFragment );
40+
Bundle bundle= new Bundle();
41+
bundle.putString( FirstFragment.KEY , "From 2" );
42+
NavHostFragment.findNavController( SecondFragment.this ).navigate( R.id.action_SecondFragment_to_FirstFragment, bundle );
4143
}
4244
} );
4345

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<string name="next">Next</string>
88
<string name="previous">Previous</string>
99

10-
<string name="hello_first_fragment">Hello first fragment</string>
10+
<string name="hello_first_fragment">Hello first fragment. Arg: %1$s</string>
1111
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
1212
</resources>

0 commit comments

Comments
 (0)