質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1455閲覧

アプリを再度立ち上げると、画面遷移が途中(5ページ目)から始まってしまう。

tsoT561T

総合スコア13

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2017/04/24 08:02

編集2017/04/24 09:19

###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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yona

2017/04/24 08:32

一度アプリを落とすとはどんな行為ですか?また、コードはコード用の記述に変更してください。
King_of_Flies

2017/04/24 08:34

追記:初心者マークが付いていたので。記述を```でくくるとコード用の記述になりますb
tsoT561T

2017/04/24 09:00

アプリを落とすということは、マルチタスクから消すということです。
yona

2017/04/24 09:12

マルチタスク?履歴のことですか?
tsoT561T

2017/04/24 09:41

コード用の記述についてご教授いただきありがとうございました。
guest

回答1

0

ベストアンサー

intent-filterが全てのActivityについていますが、この設定は必要ですか?
ここの設定によるActivityの起動時のフラグが不具合の原因だと考えられます。

投稿2017/04/24 09:17

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tsoT561T

2017/04/24 09:24

intent-filterとはどの部分のことでしょうか?
yona

2017/04/24 09:28

AndroidManifestファイルの中です。
tsoT561T

2017/04/24 09:36

AndroidManifest.xmlのFirstActivityの部分のintent-filterだけを残し、ほかのintent-filterを削除したら、画面遷移がうまくいきました。再度アプリを起動しても1ページ目から始まるようになりました。本当にありがとうございました。助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問