Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created April 17, 2012 12:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save daichan4649/2405705 to your computer and use it in GitHub Desktop.
Save daichan4649/2405705 to your computer and use it in GitHub Desktop.
DialogFragment sample
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abeshi" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showDialog(success)" />
<Button
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showDialog(error)" />
</LinearLayout>
package test.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private static final String TAG = "dialog";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 成功パターン
findViewById(R.id.success).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SuccessCaseDialogFragment.newInstance().show(getFragmentManager(), TAG);
}
});
// 失敗パターン
findViewById(R.id.error).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ErrorCaseDialogFragment.newInstance().show(getFragmentManager(), TAG);
}
});
}
private static class ErrorCaseDialogFragment extends DialogFragment {
public static DialogFragment newInstance() {
return new ErrorCaseDialogFragment();
}
}
}
package test.fragment;
import android.app.DialogFragment;
public class SuccessCaseDialogFragment extends DialogFragment {
public static DialogFragment newInstance() {
return new SuccessCaseDialogFragment();
}
}
package test.fragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TestDialogFragment extends DialogFragment {
public static DialogFragment newInstace() {
DialogFragment dialogFragment = new TestDialogFragment();
return dialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("TestDialogFragment");
builder.setView(getContentView());
Dialog dialog = builder.create();
return dialog;
}
private View getContentView() {
LayoutInflater inflater = getActivity().getLayoutInflater();
return inflater.inflate(R.layout.dialog, null);
}
}
@daichan4649
Copy link
Author

DialogFragmentのサンプル。Activity再生成(画面の回転等)してもDialog表示しっぱなし。

// コネタ
Fragmentはprivateなクラスとして定義しない。
(例えば private static TestDialogFragment extends DialogFragment ..)
Activity再生成時の自動再生成対象な場合、以下の例外発生する。

Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment XxxFragment:
make sure class name exists, is public, and has an empty constructor that is public

自動生成処理時に外部から呼び出せるようにしといて(publicコンストラクタ用意しとけ)、ということかな?

@alevis
Copy link

alevis commented Jun 11, 2015

Is it possible to make an embeddable DialogFragment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment