Created
April 17, 2012 12:35
-
-
Save daichan4649/2405705 to your computer and use it in GitHub Desktop.
DialogFragment sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.fragment; | |
import android.app.DialogFragment; | |
public class SuccessCaseDialogFragment extends DialogFragment { | |
public static DialogFragment newInstance() { | |
return new SuccessCaseDialogFragment(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
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コンストラクタ用意しとけ)、ということかな?