This is my custom Fragment:
Interface:
public interface NotVerifiedWifiCardFragment{
void loadSSIDName(String ssid);
void setArguments(Bundle bundle);
}
Class:
public class NotVerifiedWifiCardFragmentImpl extends Fragment implements NotVerifiedWifiCardFragment {
private TextView ssid;
private View view;
//@Inject
//NotVerifiedWifiCardPresenter presenter;
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_card_connected_not_verified, null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
}
private void initView() {
ssid = (TextView) view.findViewById(R.id.card_not_verified_wifi_ssid);
Bundle bundle = getArguments();
ssid.setText(bundle.getString("SSID"));
//presenter.setView(this);
}
@Override
public void loadSSIDName(String ssid) {
this.ssid.setText(ssid);
}
}
When I replace a new CustomFragment with FragmentTransaction I can do it without any problem:
@Override
public void showNotVerifiedWifiCard(String ssid) {
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.setCustomAnimations(R.anim.slide_up_anim, R.anim.slide_down_anim);
ft.replace(R.id.main_small_card, new NotVerifiedWifiCardFragmentImpl());
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
}
But when I try to call the Fragment Transaction with the same fragment but initialized (to pass a Bundle), replace method doesn't allow me. How do you solve it when you need to pass a bundle with data?
@Override
public void showNotVerifiedWifiCard(String ssid) {
Bundle bundle = new Bundle();
bundle.putString("SSID", ssid);
NotVerifiedWifiCardFragment fm = new NotVerifiedWifiCardFragmentImpl();
fm.setArguments(bundle);
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.setCustomAnimations(R.anim.slide_up_anim, R.anim.slide_down_anim);
ft.replace(R.id.main_small_card, fm);
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
}
Probably it is a silly object oriented concexpt misunderstood.
EDIT: Error show is:
Error: Wrong 2nd argument type. Found:'com.blabla.NotVerifiedWifiCardFragment', required: 'android.support.v4.app.Fragment'
If i understood your question correctly, you can try to use
public static Fragment instantiate(Context context, String fname, @Nullable Bundle args)
like this :
NotVerifiedWifiCardFragment fm = (NotVerifiedWifiCardFragment) Fragment.instantiate(context, "some_name", your_args);
Since not any object implementing the NotVerifiedWifiCardFragment
is actually a fragment (you can declare any object class and make it implement this protocol), you can't treat NotVerifiedWifiCardFragment as a Fragment (the name you gave the interface is mistaking).
For example:
class Test implements NotVerifiedWifiCardFragment {
void loadSSIDName(String ssid){}
void setArguments(Bundle bundle){}
....
}
Test is just a simple class, not a fragment.
A quick solution would be replacing
NotVerifiedWifiCardFragment fm = new NotVerifiedWifiCardFragmentImpl();
with
NotVerifiedWifiCardFragmentImpl fm = new NotVerifiedWifiCardFragmentImpl()
What you really want in that case is to create an abstract Fragment class called NotVerifiedWifiCardFragment
and make other fragments extend it.
Example code
abstract class NotVerifiedWifiCardFragment extends Fragment {
abstract void loadSSIDName(String ssid);
abstract void setArguments(Bundle bundle);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.