Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Passing Data between fragments in Android using Interface

public class FragmentA extends Fragment 
{
    DataPassListener mCallback;
    
    public interface DataPassListener{
        public void passData(String data);
    }

    @Override
    public void onAttach(Context context) 
    {
        super.onAttach(context);
        // This makes sure that the host activity has implemented the callback interface
        // If not, it throws an exception
        try 
        {
            mCallback = (OnImageClickListener) context;
        }
        catch (ClassCastException e) 
        {
            throw new ClassCastException(context.toString()+ " must implement OnImageClickListener");
        }
    }
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        // Suppose that when a button clicked second FragmentB will be inflated
        // some data on FragmentA will pass FragmentB
        // Button passDataButton = (Button).........
        
        passDataButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (view.getId() == R.id.passDataButton) {
                    mCallback.passData("Text to pass FragmentB");
                }
            }
        });
    }
}
Comment

Passing Data between fragments in Android using Interface

public class MainActivity extends ActionBarActivity implements DataPassListener{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (findViewById(R.id.container) != null) {
            if (savedInstanceState != null) {
                return;
            }
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new FragmentA()).commit();
        }
    }
    
    @Override
    public void passData(String data) {
        FragmentB fragmentB = new FragmentB ();
        Bundle args = new Bundle();
        args.putString(FragmentB.DATA_RECEIVE, data);
        fragmentB .setArguments(args);
        getFragmentManager().beginTransaction()
            .replace(R.id.container, fragmentB )
            .commit();
    }
}
Comment

Passing Data between fragments in Android using Interface

public class FragmentB extends Fragment{
    final static String DATA_RECEIVE = "data_receive";
    TextView showReceivedData;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_B, container, false);
        showReceivedData = (TextView) view.findViewById(R.id.showReceivedData);
    }
    
    @Override
    public void onStart() {
        super.onStart();
        Bundle args = getArguments();
        if (args != null) {
            showReceivedData.setText(args.getString(DATA_RECEIVE));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: running same tests against different data 
Typescript :: github:Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15 in android 
Typescript :: nest custom class validator 
Typescript :: check if that inex exisits array c# 
Typescript :: online ts compiler 
Typescript :: module svg typescript 
Typescript :: distance between two lat long points google maps api 
Typescript :: 10 elements of gothic literature 
Typescript :: What are the components of the environment? Explain it along with the examples. 
Typescript :: tiqets endpoints 
Typescript :: install vsts client version 14.102.0 
Cpp :: fast io 
Cpp :: conda list envs 
Cpp :: std::pair c++ access element 
Cpp :: How to make two dimensional string in c++ 
Cpp :: how to print the address of an object in c++ 
Cpp :: initialize vector to all zeros c++ 
Cpp :: rng c++ 
Cpp :: 2114. Maximum Number of Words Found in Sentences leetcode solution in c++ 
Cpp :: how to sort in descending order c++ 
Cpp :: arduino get size of array 
Cpp :: cpp executing without console 
Cpp :: file handling 
Cpp :: expected number of trials to get n consecutive heads 
Cpp :: c++ wait for user input 
Cpp :: c++ randomization 
Cpp :: taking input from user in array in c++ 
Cpp :: how to hide ui elements unity 
Cpp :: convert string to number c++ 
Cpp :: sfml mouse button pressed 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =