activity 传值 fragment
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn11);
Button button2 = findViewById(R.id.btn22);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn11:
Bundle bundle = new Bundle();
bundle.putString("message","课堂。。");
BlankFragment33 blankFragment33 = new BlankFragment33();
blankFragment33.setArguments(bundle);
blankFragment33.setFragmentCallback(new IFragmentCallback() {
@Override
public void sendMsgToActivity(String msg) {
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
@Override
public String getMsgFromActivity(String msg) {
return "hello,I am from activity";
}
});
replaceFragment(blankFragment33);
break;
case R.id.btn22:
}
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.framelayout11, fragment);
transaction.commit();
}
}
package com.example.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class BlankFragment33 extends Fragment {
private static final String TAG = "BlankFragment33";
private View rootview;
public BlankFragment33() {
}
private IFragmentCallback fragmentCallback;
public void setFragmentCallback(IFragmentCallback callback){
fragmentCallback = callback;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
String string = bundle.getString("message");
Log.d(TAG, "onCreate: "+ string);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootview == null){
rootview = inflater.inflate(R.layout.fragment_blank33, container, false);
}
Button button = rootview.findViewById(R.id.btn33);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentCallback.sendMsgToActivity("hello,I'm from Fragment");
}
});
Button button1 = rootview.findViewById(R.id.btn334);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = fragmentCallback.getMsgFromActivity("null");
Toast.makeText(BlankFragment33.this.getContext(),msg,Toast.LENGTH_SHORT).show();
}
});
return rootview;
}
@Override
public void onResume() {
super.onResume();
}
}
<?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/btn11"
android:text="@string/change"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn22"
android:text="@string/replace"></Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/framelayout11"
android:background="@color/teal_200"></FrameLayout>
</LinearLayout>
<?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=".BlankFragment33">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/btn33"
android:text="子传父" />
<Button
android:id="@+id/btn334"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="父传子" />
</FrameLayout>
package com.example.fragment;
public interface IFragmentCallback {
void sendMsgToActivity(String msg);
String getMsgFromActivity(String msg);
}