I am having an activity called Image activity inside it there are 2 fragments called image_display and image_change fragment, for image_activity I am getting data from an adapter(using getIntent), and it's working fine up to here. so I want to send the data (i.e URL) to the fragment but my method is not working.
my image_activity code is like this
class ImageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image)
var gi = getIntent()
var url = gi.getStringExtra("url")
val fragment = FragmentDisplayImage.newInstance(url.toString())
Toast.makeText(this,url , Toast.LENGTH_SHORT).show()
var manager =supportFragmentManager
manager.beginTransaction().commit()
}
}
and my FramentImage.kt is like
private const val ARG_PARAM1 = "url"
class FragmentDisplayImage : Fragment() {
private var param1: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString("url")
var image= view?.findViewById<ImageView>(R.id.Fragimage)
Picasso.get().load(param1).resize(700, 700).centerCrop().into(image);
Toast.makeText(context, param1.toString() , Toast.LENGTH_SHORT).show()
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_display_image, container, false)
}
companion object {
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String) =
FragmentDisplayImage().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
}
}
}
}
but still, I can't get my url here on the adapter
CodePudding user response:
I think so you're not starting your fragment. You have just created fragment object but never opened it using manager.
Please updated your ImageActivity code as follow:
var gi = getIntent()
var url = gi.getStringExtra("url")
val fragment = FragmentDisplayImage.newInstance(url.toString())
val manager: FragmentManager = supportFragmentManager
val transaction: FragmentTransaction = manager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
where R.id.container
will be Layout inside activity_image
where you want to put your fragment.
your activity_image
should have
<FrameLayout
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>