Home > Back-end >  Intent Fragment . how to used Intent In Fragment
Intent Fragment . how to used Intent In Fragment

Time:10-03

I'm using a interface to switch from recycler View to details activitas. my interface function works. position is coming. But I can't switch to Details Activity. I think the soproduct is from context. How can I solve this problem? Thank you

class OrderFragment : Fragment() , OnMovieClickListener {

private lateinit var linearLayoutManager: LinearLayoutManager
private lateinit var adapter: RvAdapter

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view : View =  inflater.inflate(R.layout.fragment_order, container, false)
    val orderApiService = OrderApiService()
    val api = orderApiService.getDataa(requireActivity())

    api.myOrdersAssigned().enqueue(object : Callback<List<BaseModel>?> {
        override fun onResponse(
            call: Call<List<BaseModel>?>,
            response: Response<List<BaseModel>?>
        ) {

            val arrayOrder = response.body()
            val layoutManager: LinearLayoutManager = LinearLayoutManager(activity)
            recyclerViewMyOrders.setLayoutManager(layoutManager)
            adapter = RvAdapter(arrayOrder as ArrayList<BaseModel>,this@OrderFragment)
            recyclerViewMyOrders.setAdapter(adapter)
            adapter.notifyDataSetChanged()

            if(response.isSuccessful){
                response.body()?.let {

                }
            }
        }

        override fun onFailure(call: Call<List<BaseModel>?>, t: Throwable) {
            print(t.message.toString())
        }
    })

    return view
}

override fun onMovieItemClicked(position: Int) {
    println("Clicked  :  "   position.toString())
    val intent = Intent(requireContext().applicationContext,DetailsActivity::class.java)
    startActivity(intent)
}

}

My Interface Function :

override fun onMovieItemClicked(position: Int) {
    println("Clicked  :  "   position.toString())
    val intent = Intent(requireContext().applicationContext,DetailsActivity::class.java)
    startActivity(intent)
}

CodePudding user response:

Create your variable Context inside your Fragment like this :

   private Context context;

Than you initialize it inside onCreateView like this :

context = view.getContext();

And insite you functio, instead of calling requireContext().applicationContext as parameter, call context

The code in on Java but you can easily convert it to Kotlin

CodePudding user response:

I have a question first: why don't you use the DetailFragment instead of a whole new Activity (because your OrderFragment and Detail can be both in a single Activity)

Still want to use Activity

On your interface's override method, try to change requireContext().applicationContext to requireContext() only. Please reply me your println works or not.

Change to Fragment

I know 2 ways to move to another fragment programmatically:

parentFragmentManager.beginTransaction()
      .replace<DestinationFragment>(R.id.fragmentContainer)

findNavController().navigate(action) // if you are using Navigation Components

P/s:

I'm also a Android newbie and I see something can be improved in your source code, please correct me if I have something wrong

  • Adapter and LinearLayoutManager don't have to be a class's property because you no longer need to use them outside API call, so just change their scope to inside onResponse()
  • I have suffered from doing things with view in onCreateView() (because they are not fully inflated yet ?), so considering move your logic to onViewCreated() for a safe bet.
  • Related