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

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

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

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

0回答

2994閲覧

Bottom Navigation Activityを選択して作成したアプリ内で、フラグメント内で個別のアクティビティを実行する方法について

退会済みユーザー

退会済みユーザー

総合スコア0

Java

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

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/03/01 10:47

編集2020/03/01 18:19

前提・実現したいこと

Androidアプリ開発で、最初のプロジェクト選択でBottom Navigation Activityを選び、デフォルトの「home・dashboard・notification」のフラグメントのページで、文字列を表示させるだけでなく、フラグメント内でMainActivityで指定するようなアクティビティを実行しようとしています。

具体的には、デフォルトのdashboardで、ボタンを表示し、画面の遷移を行うということを実行したいです。

画面遷移に関しては、参考記事・[Android & Kotlin] Activityの遷移をさせるの通りに、Empty Activityを選択してプロジェクトを作成した場合は、エラーなく実行することができています。

ファイルの階層構造
イメージ説明

発生している問題・エラーメッセージ

画面遷移のアクティビティをdashboardフラグメントに組み込もうとしたところ、2つの問題が発生しました。

①個別のアクティビティを指定する(今回の場合は画面遷移)MainActivity.ktを、DashboardFragment.ktにどのように組み込めば良いかわからない

DashboardFragment.ktとDashboardViewModel.ktによって、dashboardフラグメントをクリックすると「This is dashboard Fragment」という文字列が出ていることは理解しています。

クラスの定義が Fragment()となっているDashboardFragment.ktには、AppCompatActivity()のアクティビティは書き込めないと思うので、どのようにフラグメントに組み込みたいMainActivity.ktとDashboardFragment.ktとDashboardViewModel.ktを組み合わせればいいのか、わからず困っています。

②個別のアクティビティを指定する(今回の場合は画面遷移)SubActivity.ktについて

2-1. AndroidManifest.xmlでフラグメントと組み合わせる場合は、現在の書き方で正しいのかどうか

2-2. SubActivity.ktという名称で現在の階層構造のようにuiフォルダ下の各フラグメントのファイルにおいていいのか

エラーは出ていないが、似たようなことをやっているコードを見たことがないため、参考になる記事などがあれば教えていただきたいです。

該当のソースコード

フラグメントに組み込みたいMainActivity.kt

kotlin

1package your.package.name 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.content.Intent 6import kotlinx.android.synthetic.main.activity_main.* 7 8class MainActivity : AppCompatActivity() { 9 10 override fun onCreate(savedInstanceState: Bundle?) { 11 super.onCreate(savedInstanceState) 12 setContentView(R.layout.activity_main) 13 14 sendButton.setOnClickListener { 15 val intent = Intent(application, SubActivity::class.java) 16 startActivity(intent) 17 } 18 } 19}

DashboardFragment.kt

Kotlin

1package com.example.fragact.ui.dashboard 2 3import android.os.Bundle 4import android.view.LayoutInflater 5import android.view.View 6import android.view.ViewGroup 7import android.widget.TextView 8import androidx.fragment.app.Fragment 9import androidx.lifecycle.Observer 10import androidx.lifecycle.ViewModelProviders 11import com.example.fragact.R 12 13class DashboardFragment : Fragment() { 14 15 private lateinit var dashboardViewModel: DashboardViewModel 16 17 override fun onCreateView( 18 inflater: LayoutInflater, 19 container: ViewGroup?, 20 savedInstanceState: Bundle? 21 ): View? { 22 dashboardViewModel = 23 ViewModelProviders.of(this).get(DashboardViewModel::class.java) 24 val root = inflater.inflate(R.layout.fragment_dashboard, container, false) 25 val textView: TextView = root.findViewById(R.id.text_dashboard) 26 dashboardViewModel.text.observe(this, Observer { 27 textView.text = it 28 }) 29 return root 30 } 31}

DashboardViewModel.kt

kotlin

1package com.example.fragact.ui.dashboard 2 3import androidx.lifecycle.LiveData 4import androidx.lifecycle.MutableLiveData 5import androidx.lifecycle.ViewModel 6 7class DashboardViewModel : ViewModel() { 8 9 private val _text = MutableLiveData<String>().apply { 10 value = "This is dashboard Fragment" 11 } 12 val text: LiveData<String> = _text 13}

app/java/com.example.fracact/ui/dashboard/
SubActivity.kt

package com.example.fragact.ui.dashboard import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_sub.* class SubActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_sub) returnButton.setOnClickListener{ finish() } } }

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.fragact"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <activity 13 android:name=".MainActivity" 14 android:label="@string/app_name"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity 22 android:name=".SubActivity" 23 android:label="@string/app_name" > 24 </activity> 25 </application> 26 27</manifest>

試したこと

DashboardFragment.ktなどアクティビティに関わるプログラムを変更せず、layoutだけ変更した場合、アクティビティなしの遷移前の画面は表示することができました。

fragment_dashboard.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:id="@+id/text_dashboard" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_marginStart="8dp" 12 android:layout_marginTop="8dp" 13 android:layout_marginEnd="8dp" 14 android:textAlignment="center" 15 android:textSize="20sp" 16 app:layout_constraintEnd_toEndOf="parent" 17 app:layout_constraintStart_toStartOf="parent" 18 app:layout_constraintTop_toTopOf="parent" /> 19 20 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 21 xmlns:tools="http://schemas.android.com/tools" 22 android:layout_width="match_parent" 23 android:layout_height="match_parent" 24 android:orientation="vertical" 25 android:gravity="center" 26 android:background="#dfe" 27 tools:context=".MainActivity"> 28 29 <TextView 30 android:text="@string/main" 31 android:textSize="30sp" 32 android:layout_margin="10dp" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" /> 35 36 <Button 37 android:id="@+id/sendButton" 38 android:text="@string/move" 39 android:textSize="30sp" 40 android:layout_margin="20dp" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" /> 43 44 </LinearLayout> 45 46 47</androidx.constraintlayout.widget.ConstraintLayout>

コメントを受けて試したこと

app/java/com.example.fragact/MainActivityに組み込みたいMainActivity.ktの内容を追記してみました。

MainActivity

package com.example.fragact import android.os.Bundle import com.google.android.material.bottomnavigation.BottomNavigationView import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController import android.content.Intent import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navView: BottomNavigationView = findViewById(R.id.nav_view) val navController = findNavController(R.id.nav_host_fragment) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) sendButton.setOnClickListener { val intent = Intent(application, SubActivity::class.java) startActivity(intent) } } }

しかし、

sendButton.setOnClickListener { val intent = Intent(application, SubActivity::class.java) startActivity(intent) }

sendButtonUnresolved referenceとなります。

layoutのactivity_main.xmlではなく、fragment_dashboard.xmlで「sendButton」を定義していることも原因かと考えられますが、フラグメントごとにアクティビティやレイアウトを設定するには他に方法がわからない状態です。

<Button android:id="@+id/sendButton" android:text="@string/move" android:textSize="30sp" android:layout_margin="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" />

補足情報(FW/ツールのバージョンなど)

Android Studio 3.5.3

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

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

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

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

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

hoshi-takanori

2020/03/01 11:35

時間がなくてコードは見てませんが、Android プログラミングにおいて Fragment と Activity の連携は鬼門のひとつ(他にもたくさんあるけど)ですね。 とりあえず、単純に MainActivity の foo メソッドを呼び出すだけなら (activity as MainActivity).foo() で行けるのでは。(でもタイミングによってぬるぽが出るような…。)
hoshi-takanori

2020/03/02 01:22

質問文を読み直しましたが、Dashboard フラグメントにボタンを配置して、それが押されたら別のアクティビティ (SubActivity) を開きたいということでしょうか。それなら、MainActivity は関与させずに Dashboard フラグメントだけで完結します。 とはいえ、質問文を読むとそもそも Android における Activity や Fragment というのが何なのかあまり理解されていないように見受けられるので、そこらへんの理解が先な気もします。Bottom Navigation の雛形には ViewModel や Nagivation といった最新技術が駆使されているので…。
退会済みユーザー

退会済みユーザー

2020/03/02 07:52

Dashboard フラグメントにボタンを配置して、それが押されたら別のアクティビティ (SubActivity) を開きたいということです。「ActivityとFragmentの連携を理解する (Android Kotlin)」(https://101010.fun/posts/android-try-fragment.html)という記事は読みましたが、 Android における Activity や Fragment について理解するのにおすすめの記事等ありましたらご教示願います。
hoshi-takanori

2020/03/02 10:49

とりあえず Qiita から検索してみました。 Android はじめてのFragment https://qiita.com/Reyurnible/items/dffd70144da213e1208b Android はじめてのFragment イベント編 https://qiita.com/Reyurnible/items/d6397f5fbb03ee4fb93b ActivityとFragmentのライフサイクルと罠 https://qiita.com/chibi929/items/78f0d3aa2ab4a0229978 ActivityとFragment、両方向の操作 https://qiita.com/PiyoMoasa/items/9ddd7b9e4814da6ca72f Activity,Fragmentの分割と協調動作の具体例 / Androidアプリを開発する際の俺的設計 https://qiita.com/eaglesakura/items/2ab707755f5a504d9cf9
退会済みユーザー

退会済みユーザー

2020/03/02 13:04 編集

ご教示いただきましてありがとうございます。 画面遷移であれば、「Activity と Fragment」、「Fragment と Fragment」の間で可能ということが分かったのですが、今回のようにメニューバーと組みあせて画面遷移開始時点がFragmentの場合、画面遷移後のプログラムはActivityとFragmentのどちらで書くのがいいのでしょうか。 ActivityがFragmentを持つという理解なので、メニューバーを操作しているMainActivity がある上で、DashboardFragment.ktがSubActivityに繋がるよりは、DashboardFragment2というようにFragmentで画面遷移を実現した方がいいのでしょうか。
退会済みユーザー

退会済みユーザー

2020/03/03 08:14

回答ありがとうございます。Navigationファイルというものは、ActionとFragmentとどのように関係しているのでしょうか。「Activity と Fragment」、「Fragment と Fragment」はNavigation Activityとは別でサンプルコードを動かしてみました。 しかし、Bottom Navigation Activityとの組み合わせの実装は実際コードを動かしてみないと理解できない部分も多いのですが、書き方がわからずどうすればいいかわからずコードが書けません。現在実現したいことのサンプルコードなどはご存知ないでしょうか。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問