-4

So what i want is to launch a new activity (ProfileActivity) when user selects "Profile" from overflow menu, and i want to pass some data to that ProfileActivity at the same time. How to properly transfer data from a fragment to another activity that is not a container of the fragment itself? I do the following:

1- Create an interface

interface IProfileToActivity {
    fun profileInfo(data: AllHeroes.Global)
}

2- Then I inheritance in the activity

class ProfileActivity : AppCompatActivity(), IProfileToActivity {
private lateinit var myBinding: ActivityProfileBinding
    override fun profileInfo(data: AllHeroes.Global) {
        myBinding.tvUsername.text = data.name
        myBinding.tvDivision.text = data.rank.rankName
        Log.i("Apex Info 3", data.toString())
    }
}

3- sending from a fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
  (activity as? IProfileToActivity)?.profileInfo(allInfoApexResponse.global)
                mHeroesAdapter.heroesList(allAdapterListHero)
}

The data should be transferred to another activity after clicking the Profile button in the menu

   override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId){
            R.id.action_profile -> {
                (activity as? IProfileToActivity)?.profileInfo(testApex)
                startActivity(Intent(requireActivity(), ProfileActivity::class.java))
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

but nothing happens, why? what did I do wrong?

Ethernets
  • 201
  • 2
  • 12
  • `How to properly transfer data from a fragment to another activity that is not a container of the fragment itself?` activities contain fragments, so _somewhere_ you're starting this activity with an intent, right ? pass data to that intent – a_local_nobody Nov 04 '21 at 09:54
  • @a_local_nobody do I need to form a bundle? or just how to send intent? how correct will it be if I do it from the fragment itself? – Ethernets Nov 04 '21 at 09:57
  • 2
    https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – a_local_nobody Nov 04 '21 at 10:08
  • 3
    Does this answer your question? [Passing Data Between Fragments to Activity](https://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity) – Abhishek Dutt Nov 04 '21 at 10:38

1 Answers1

2

if you're routing from fragment to another activity, you can put you data into the intent using the putExtra and then receive in the activity using getExtra.

Inside the fragment,

Intent profileActivityIntent = new Intent(context,ProfileActivity.class);
profileActivityIntent.putExtra("dataKey",data);
startActivity(profileActivityIntent);

And then inside the ProfileActivity's onCreate method,

//assuming that data is a string
String dataFromFragment = getIntent().getStringExtra("dataKey");
Log.i("Data from fragment",dataFromFragment);

You are in no need of using the interface method. (If you have to route from one fragment to activity).

desertnaut
  • 57,590
  • 26
  • 140
  • 166