###Android studioで画面遷移するアプリを作りたい
Android studioで画面遷移するアプリを製作しています。このプログラムはボタンを押すと次の画面へ移行するというものです。画面は6ページあります。
https://akira-watson.com/android/activity-1.html
https://akira-watson.com/android/activity-2.html
上記のサイトを参考に制作しました。
###発生している問題→アプリを一度落としてから再度開くと途中のページから始まってしまう(原因がわからない)
Android studioでRunボタンを押し実機でアプリを立ち上げると、正常に作動し、1ページ目から6ページ目まで順に遷移できます。しかし、一度アプリを落とし、再度実機でアプリを開くと5ページ目からのスタートになってしまいます。
###該当のソースコード
FirstActivity.java
java
1import android.content.Intent; 2import android.os.Bundle; 3import android.support.v4.app.FragmentActivity; 4 5public class FifthActivity extends FragmentActivity{ 6 @Override 7 protected void onCreate(Bundle savedInstanceState){ 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.fifth); 10 } 11 12 void move(){ 13 Intent intent = new Intent(getApplication(), SixthActivity.class); 14 startActivity(intent); 15 } 16}
FirstFragment.java
java
1import android.content.Context; 2import android.os.Bundle; 3import android.support.v4.app.Fragment; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import android.widget.Button; 8 9public class FifthFragment extends Fragment{ 10 FifthActivity parent = new FifthActivity(); 11 12 @Override 13 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 14 View view = inflater.inflate(R.layout.fifth_fragment, container, false); 15 Button btnMove = (Button) view.findViewById(R.id.next5); 16 btnMove.setOnClickListener(new View.OnClickListener() { 17 @Override 18 public void onClick(View v) { 19 parent.move(); 20 } 21 }); 22 return view; 23 } 24 25 @Override 26 public void onAttach(Context context){ 27 parent = (FifthActivity) context; 28 super.onAttach(context); 29 } 30}
first.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 android:id="@+id/layout_root" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="fill_parent" 7 android:layout_height="fill_parent"> 8 9 <fragment 10 android:id="@+id/fragment5" 11 android:name="com.example.students.pelmanism.FifthFragment" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" /> 14 15</RelativeLayout>
first_fragment.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 android:id="@+id/layout_root" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="fill_parent" 7 android:layout_height="fill_parent"> 8 9 <TextView 10 android:id="@+id/text1" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_alignParentLeft="true" 14 android:layout_alignParentStart="true" 15 android:layout_alignParentTop="true" 16 android:text="First page" 17 android:textSize="30sp" /> 18 19 <Button 20 android:id="@+id/next1" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_centerHorizontal="true" 24 android:layout_centerVertical="true" 25 android:text="next" 26 android:textColor="@android:color/black" /> 27 28</RelativeLayout>
以下、5ページ目まで同様です。
java
1**SixthActivity.java** 2import android.os.Bundle; 3import android.support.v4.app.FragmentActivity; 4 5public class SixthActivity extends FragmentActivity{ 6 @Override 7 protected void onCreate(Bundle savedInstanceState){ 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.sixth); 10 } 11} 12
SixthFragment.java
java
1import android.os.Bundle; 2import android.support.v4.app.Fragment; 3import android.view.LayoutInflater; 4import android.view.View; 5import android.view.ViewGroup; 6 7public class SixthFragment extends Fragment{ 8 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 9 View view = inflater.inflate(R.layout.sixth_fragment, container, false); 10 return view; 11 } 12}
sixth.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 xmlns:tools="http://schemas.android.com/tools" 4 xmlns:andriod="http://schemas.android.com/apk/res/android" 5 tools:context="com.example.students.pelmanism.SixthActivity" 6 andriod:layout_width="match_parent" 7 tools:ignore="NamespaceTypo" 8 andriod:layout_height="match_parent"> 9 10 <fragment 11 andriod:id="@+id/fragment6" 12 andriod:name="com.example.students.pelmanism.SixthFragment" 13 andriod:layout_width="match_parent" 14 andriod:layout_height="match_parent" /> 15 16</RelativeLayout>
sixth_fragment.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 android:id="@+id/layout_root" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="fill_parent" 7 android:layout_height="fill_parent"> 8 9 <TextView 10 android:id="@+id/text6" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_centerHorizontal="true" 14 android:layout_centerVertical="true" 15 android:text="Sixth page" /> 16 17 18</RelativeLayout>
AndroidManifest.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.students.pelmanism" 4 android:versionCode="1" 5 android:versionName="1.0"> 6 7 <user-sdk 8 android:minSdkVersion="11" 9 android:targetSdkVersion="19"/> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@mipmap/ic_launcher" 14 android:label="@string/app_name" 15 android:roundIcon="@mipmap/ic_launcher_round" 16 android:supportsRtl="true" 17 android:theme="@style/AppTheme"> 18 <activity 19 android:name="com.example.students.pelmanism.FirstActivity" 20 android:label="@string/app_name"> 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN"/> 23 <category android:name="android.intent.category.LAUNCHER"/> 24 </intent-filter> 25 </activity> 26 <activity 27 android:name="com.example.students.pelmanism.SecondActivity" 28 android:label="@string/app_name"> 29 <intent-filter> 30 <action android:name="android.intent.action.MAIN"/> 31 <category android:name="android.intent.category.LAUNCHER"/> 32 </intent-filter> 33 </activity> 34 <activity 35 android:name="com.example.students.pelmanism.ThirdActivity" 36 android:label="@string/app_name"> 37 <intent-filter> 38 <action android:name="android.intent.action.MAIN"/> 39 <category android:name="android.intent.category.LAUNCHER"/> 40 </intent-filter> 41 </activity> 42 <activity 43 android:name="com.example.students.pelmanism.FourthActivity" 44 android:label="@string/app_name"> 45 <intent-filter> 46 <action android:name="android.intent.action.MAIN"/> 47 <category android:name="android.intent.category.LAUNCHER"/> 48 </intent-filter> 49 </activity> 50 <activity 51 android:name="com.example.students.pelmanism.FifthActivity" 52 android:label="@string/app_name"> 53 <intent-filter> 54 <action android:name="android.intent.action.MAIN"/> 55 <category android:name="android.intent.category.LAUNCHER"/> 56 </intent-filter> 57 </activity> 58 <activity 59 android:name="com.example.students.pelmanism.SixthActivity" 60 android:label="Pelmanism"> 61 </activity> 62 </application> 63</manifest>
###試したこと
全ての○○Activity.javaに対し、...new Intent(this,...になっていたのを...new Intent(getApplication(),...に直しました。
java
1Intent intent = new Intent(getApplication(), ○○Activity.class);
上記のようにしても変化はありませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
言語はjava
Android studio 2.3.1
実機はNEXUS7
回答1件
あなたの回答
tips
プレビュー