Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements
- MyCustomDialog.OnInputSelected{
- public String dialogInput;
- FragmentManager fragmentManager;
- @Override
- public void sendInput(String input) {
- dialogInput = input;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- fragmentManager = getSupportFragmentManager();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- //Inflate the menu, this adds items to the action bar if it is present
- getMenuInflater().inflate(R.menu.menu, menu);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- //Handle action bar clicks here. The action bar will automatically handle clicks on the home/up button
- //so long as you specify a parent activity in AndroidManifest.xml
- switch(item.getItemId()){
- case R.id.action_add:
- MyCustomDialog dialog = new MyCustomDialog();
- dialog.show(getSupportFragmentManager(), "MyCustomDialog");
- //Trying Bundle to pass data, dialog input between activity and fragment
- Bundle bundle = new Bundle();
- bundle.putString("Dialog Input", dialogInput);
- //Set Fragment class arguments
- MainFragment fragment = new MainFragment();
- fragment.setArguments(bundle); //set argument bundle to fragment
- fragmentManager.beginTransaction().replace(R.id.MainFragment,fragment).commit(); //now replace Mainfragment
- Toast.makeText(this, "Action_Add Clicked Successfully", Toast.LENGTH_SHORT).show();
- }
- return super.onOptionsItemSelected(item);
- }
- }
- public class MainFragment extends Fragment implements MyCustomDialog.OnInputSelected{
- TextView InputDisplay;
- Button OpenDialog;
- @Nullable
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_main, container, false);
- InputDisplay = view.findViewById(R.id.InputDisplay);
- OpenDialog = view.findViewById(R.id.Open_Dialog);
- //Getting Main Activity dialog information with Bundle, that was received from toolbar add
- Bundle bundle = getArguments();
- if(bundle != null){
- String dialogInput = bundle.toString();
- //Clearing since Fragment call and activity call overlap each other.
- InputDisplay.setText("");
- InputDisplay.clearComposingText();
- InputDisplay.setText(dialogInput);
- }
- //String dialogInput = this.getArguments().getString("Dialog Input");
- OpenDialog.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Log.d("MainFragment", "onClick: opening dialog");
- MyCustomDialog customDialog = new MyCustomDialog();
- customDialog.setTargetFragment(MainFragment.this, 1);
- customDialog.show(getFragmentManager(), "MyCustomDialog");
- }
- });
- return view;
- }
- @Override
- public void sendInput(String input) {
- InputDisplay.setText("");
- InputDisplay.setText(input);
- }
- }
- public class MyCustomDialog extends DialogFragment {
- private EditText Input;
- private TextView ActionOK, ActionCANCEL;
- private OnInputSelected onInputSelected;
- public interface OnInputSelected{
- void sendInput(String input);
- }
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- try{
- Fragment onInputSelected_fragment = getTargetFragment();
- Activity onInputSelected_activity = getActivity();
- if(onInputSelected_fragment != null){
- onInputSelected = (OnInputSelected) onInputSelected_fragment;
- }else{
- onInputSelected = (OnInputSelected) onInputSelected_activity;
- }
- //throw new RuntimeException("Custom Dialog onAttach Listener was NULL");
- }catch(ClassCastException e){
- Log.e("Custom Dialog", "onAttach: ClassCastException: " + e.getMessage());
- }
- }
- @Nullable
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.dialog_my_custom, container, false);
- Input = view.findViewById(R.id.Input);
- ActionOK = view.findViewById(R.id.Action_OK);
- ActionCANCEL = view.findViewById(R.id.Action_CANCEL);
- ActionCANCEL.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- getDialog().dismiss();
- }
- });
- ActionOK.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onInputSelected.sendInput(Input.getText().toString());
- getDialog().dismiss();
- }
- });
- return view;
- }
- }
Add Comment
Please, Sign In to add comment