Reputation: 513
I am making an app using Android Studio where you can listen to downloaded music, and upload music to the cloud. Currently, I am trying to make a button to add a playlist. Here is the home fragment, where the add button is located:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Home Fragment"
android:textSize="30sp" />
<ImageButton
android:id="@+id/new_playlist"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:background="@drawable/shadow"
android:elevation="10dp"
android:src="@drawable/ic_new" />
</RelativeLayout>
As seen above, the button is the ImageButton icon. Here is the code for adding the onClick event:
@Override
protected void onStart() {
super.onStart();
ImageButton imgBtn = findViewById(R.id.new_playlist);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new NewPlaylistFragment()).commit();
}
});
}
This works fine, displaying the NewPlaylistFragment. The issue is when I use the navigation menu and click on the home fragment. When attempting to hit the new playlist button a second time, nothing happens. When replacing the fragment stuff with a simple Toast message, it fires every time I hit the button. After doing more investigation, I found that changing to the profile fragment and coming back to the home fragment renders the new playlist button useless, regardless the code inside the onclick function. In case it helps, here is the code for the navigation redirection:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_email:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
startActivity(emailIntent);
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
My only guess is that changing fragments causes the onClick event to disappear, but I don't know how to fix this.
Edit
I tried adding the onClick when a new home fragment object is created, but the problem still persisted.
Upvotes: 1
Views: 51
Reputation: 12478
You findViewById(R.id.new_playlist)
from the first instantiated HomeFragment
.
You must find it (and setOnClickListener
) every time you instantiate a new one.
case R.id.nav_home:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new HomeFragment())
.commit();
imgBtn = findViewById(R.id.new_playlist);
imgBtn.setOnClickListener(onClickListner);
break;
Above is a quick fix. But ideally, I think, you should not handle the finding of the Button
and setOnClickListener
in your Activity
but in your HomeFragment
class.
Additionally, about your code, every time R.id.nav_home
or R.id.nav_profile
selected, a new HomeFragment
or a new ProfileFragment
will be spawned.
To avoid multiple spawning of Fragment, use a tag String and findFragmentByTag
(or findFragmentById
).
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment homeFragment = fragmentManager.findFragmentByTag(HomeFragment.TAG);
if (homeFragment == null) {
homeFragment = new HomeFragment();
}
fragmentManager
.beginTransaction()
.replace(R.id.fragment_container, homeFragment, HomeFragment.TAG)
.addToBackStack(null)
.commit();
(In the above sample, the HomeFragment.TAG is a String constant (static final String).
Upvotes: 1