0

when I am trying to click on an element on the reycler view it creates a new intent in the fragment and calls a different Activity but when it starts the new activity from the intent it just doesnt display anything or the breakpoint at SecondaryActivity shows up

this code is running from a fragment on the main activity and the new activity isnt displaying any layout

   fun loadImages(){
        recyclerView!!.setHasFixedSize(true)
        recyclerView!!.layoutManager = GridLayoutManager(this.activity,4)

        try {
            this.imageList = ImageGallery.GetImagesList(this.context as Context )
        }
        catch (ex:Exception)
        {
            println(ex.message)
        }

       try { galleryAdapter = GalleryAdapter(this.context as Context,imageList,
           object : IPhotoInterface {
               override fun onPhotoClick(stringPath: String):Unit {
                   //process picture on click
                   imageIntent  = android.content.Intent(context,SecondaryActivity::class.java)
                   imageIntent.putExtra("image_file",File(stringPath))
                   try{
                       startActivity(imageIntent)//here throws and exception

                   }catch(except:Exception)
                   {
                       println(except.message)
                   }

               }
           })
           recyclerView?.adapter = galleryAdapter
           galleryNumberText?.text = "${imageList.size} images"
       }
        catch(ex:Exception)
        {
            println(ex.message)
        }
    }

secondary_activity.xml


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <include layout="@layout/secondary_action_bar"></include>
       <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
           android:background="@color/grey">
    
           <ImageView
               android:id="@+id/imageView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/gearicon"
               app:flow_verticalAlign="center"
               tools:ignore="MissingConstraints">
    
           </ImageView>
    
    
           <com.google.android.material.bottomnavigation.BottomNavigationView
               android:layout_width="407dp"
               android:id="@+id/bottomNavBar"
               android:layout_height="57dp"
               android:layout_marginTop="32dp"
               app:layout_constraintBottom_toBottomOf="parent"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintHorizontal_bias="0.461"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintVertical_bias="1.0"
               android:background="@color/black"
               app:menu="@menu/bottom_navigation_menu" />
    
    
       </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.drawerlayout.widget.DrawerLayout>

SecondaryActivity

kt
class SecondaryActivity: AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
            super.onCreate(savedInstanceState, persistentState)
        val i = intent
        val myParcelableObject: File? =
            i.getParcelableExtra<Parcelable>("image_file") as File?
    }
}
PontiacGTX
  • 185
  • 2
  • 15
  • @MikeM. okI will add it to the OP – PontiacGTX Mar 31 '22 at 17:39
  • You've overridden the wrong `onCreate()`. You want the one _without_ the `PersistableBundle` parameter: https://stackoverflow.com/q/60427204, https://stackoverflow.com/q/54437922. – Mike M. Mar 31 '22 at 17:40
  • @MikeM. you are right, so i wonder why when i pass my Parcelable with a variable defined it doesnt show up anything? the variable is defined as a empty string? – PontiacGTX Mar 31 '22 at 17:46
  • I'm not sure if I understand exactly what you're asking, but why are you trying to pass a `File` instead of a simple `String`, the `stringPath`? You can create the `File` instance in `SecondaryActivity`, if that's where you need it. – Mike M. Mar 31 '22 at 17:48
  • Wait, do you mean that you're still not seeing the layout? If so, you're not calling `setContentView()` in `SecondaryActivity`. I figured you'd omitted it only here for brevity. – Mike M. Mar 31 '22 at 17:51
  • @MikeM. I set a Parcelable object which I have used a constructor with a var variable for a string but on the activity is showing that the path is empty... – PontiacGTX Mar 31 '22 at 19:36
  • on imageIntent.putExtra("image_file",File(stringPath)) – PontiacGTX Mar 31 '22 at 19:38
  • I think the corret way to retrieve is using intent.extras instead intent – PontiacGTX Mar 31 '22 at 19:44
  • I'm not sure how you're determining that, since the `SecondaryActivity` in the question isn't really doing anything, but `File` is not `Parcelable`, so `getParcelableExtra()` is not the right function to retrieve it. It is `Serializable`, however, so `getSerializableExtra()` would be the function to use. However, as I alluded to earlier, it would be preferable to just pass the `String`, and create the `File` later. – Mike M. Mar 31 '22 at 19:44
  • @MikeM. I have inherited from Parcelable to create a File class, only that the functionn I was using on intent wasnt retrieving the data correctly – PontiacGTX Mar 31 '22 at 19:47
  • You should really change the name to something other than `File` to prevent confusion with the Java class, 'cause it's going to be _very_ easy to get those mixed up. In any case, it's still preferable to pass a simple `String` on the `Intent`, instead of some heavier object, no matter whether it's your `File` class or Java's. – Mike M. Mar 31 '22 at 19:50
  • @MikeM. i looked before and i have found the only way to pass a value from a fragment on a different activity a new (different) activity was using an intent and either a Serializable or a Parcalable still I dont get how you can pass it? just set on putExtra? – PontiacGTX Mar 31 '22 at 19:54
  • `Serializable` and `Parcelable` are only necessary if you have to pass complex objects like that. You only need to pass a `String`, though, and there are built-in methods for that (and also for all of the primitives, like `int` and `boolean`). Change the first part to `imageIntent.putExtra("image_file", stringPath)`, then in the second `Activity` use `val stringPath = intent.getStringExtra("image_file")` and then create the `File` object there, if you really need it. If you don't really need that, just use the `String` for whatever. – Mike M. Mar 31 '22 at 20:00
  • but how come it created an instance with a field set on the previous fragment in the main activity but then when intializing the parcelable object has a empty string?? similar to this https://stackoverflow.com/questions/49473416/intent-putextra-passes-empty-value – PontiacGTX Mar 31 '22 at 20:03
  • I'm sorry, but I don't really understand what you're asking, and there's really not much more I could tell you from the given code anyway. If you need specifics, you'll have to provide a [mcve], including the `import` statements, since you have conflicting class names. – Mike M. Mar 31 '22 at 20:09
  • I will try renaming the class edit: It wasnt a conflicting class name.. so i had to be something else – PontiacGTX Mar 31 '22 at 20:11

0 Answers0