Androidで標準APIのFragmentを使用し、Fragmentの動的変更をバックボタンで元に戻せるようにします。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample53-005-FragmentBackButton」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample53_005 |
| プロジェクト名(Project Name) | AndroidSample53-005-FragmentBackButton |
| パッケージ名(Package Name) | com.example.androidsample53_005 |
| Minimum Required SDK | API 14 |
| Target SDK | API 14 |
| Compile With | API 14 |
| Theme | Theme Holo Light |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample53_005"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="AndroidSample53-004"
android:theme="@style/AppTheme" >
<activity android:name="com.example.androidsample53_005.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/values/styles.xml」は以下の通り。
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light" ></style>
</resources>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「res/layout/activity_main.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample53_005"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="AndroidSample53-005"
android:theme="@style/AppTheme" >
<activity android:name="com.example.androidsample53_005.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「任意のファイルの作成方法」の手順で「res/layout/fragment1.xml」を作成。
2.「res/layout/fragment1.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:textColor="@android:color/black" />
</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「src/com/example/androidsample53_005/MainActivity.java」は以下の通り。
package com.example.androidsample53_005;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private Fragment1 fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickAdd(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (this.fragment1 == null) {
this.fragment1 = new Fragment1();
}
fragmentTransaction.add(R.id.fragment_container, this.fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
findViewById(R.id.buttonAdd).setEnabled(false);
findViewById(R.id.buttonRemove).setEnabled(true);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順7】
1.「src/com/example/androidsample53_005/Fragment1.java」は以下の通り。
package com.example.androidsample53_005;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順8】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順9】
1.以下の様に表示されれば成功です。
追加ボタンでFragmentを追加します。
バックボタンでFragmentが追加される前に戻ります。
※その他のボタンの状態は戻りません。
以上です。


0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。