1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.supportv4.app;
18 
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.View.OnClickListener;
23 import android.view.ViewGroup;
24 import android.widget.Button;
25 import android.widget.TextView;
26 
27 import androidx.fragment.app.DialogFragment;
28 import androidx.fragment.app.FragmentActivity;
29 import androidx.fragment.app.FragmentTransaction;
30 
31 import com.example.android.supportv4.R;
32 
33 public class FragmentDialogOrActivitySupport extends FragmentActivity {
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         setContentView(R.layout.fragment_dialog_or_activity);
38 
39         if (savedInstanceState == null) {
40             // First-time init; create fragment to embed in activity.
41 //BEGIN_INCLUDE(embed)
42             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
43             DialogFragment newFragment = MyDialogFragment.newInstance();
44             ft.add(R.id.embedded, newFragment);
45             ft.commit();
46 //END_INCLUDE(embed)
47         }
48 
49         // Watch for button clicks.
50         Button button = (Button)findViewById(R.id.show_dialog);
51         button.setOnClickListener(new OnClickListener() {
52             @Override
53             public void onClick(View v) {
54                 showDialog();
55             }
56         });
57     }
58 
59 //BEGIN_INCLUDE(show_dialog)
showDialog()60     void showDialog() {
61         // Create the fragment and show it as a dialog.
62         DialogFragment newFragment = MyDialogFragment.newInstance();
63         newFragment.show(getSupportFragmentManager(), "dialog");
64     }
65 //END_INCLUDE(show_dialog)
66 
67 //BEGIN_INCLUDE(dialog)
68     public static class MyDialogFragment extends DialogFragment {
newInstance()69         static MyDialogFragment newInstance() {
70             return new MyDialogFragment();
71         }
72 
73         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)74         public View onCreateView(LayoutInflater inflater, ViewGroup container,
75                 Bundle savedInstanceState) {
76             View v = inflater.inflate(R.layout.hello_world, container, false);
77             View tv = v.findViewById(R.id.text);
78             ((TextView)tv).setText("This is an instance of MyDialogFragment");
79             return v;
80         }
81     }
82 //END_INCLUDE(dialog)
83 }
84