我的MainActivity中有一个“先问密码”功能,当用户从一个片段切换到另一个片段时,我想调用该功能。我想知道我的函数是否可以接受片段作为参数,然后将其传递给该函数。
我已经尝试过将字符串传递给函数并使用findFragmentByTag,但它返回null
private void logIn(final String getfrag){
Fragment fragment = getSupportFragmentManager().findFragmentByTag(getfrag);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment)
.commit();
}
这是我的代码的样子
private returnType?? logIn(fragmentparameterhere???????){
//show alert dialog editText and ask for password
alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = String.valueOf(input.getText());
if (value.equals("123456")){
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Welcome!", Toast.LENGTH_SHORT).show();
Fragment fragment = fragmentparameterhere??;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment)
.commit();
}
else {
x[0]++;
if(x[0] == 5){
//password entered too many times
}
else {
//toast wrong password
}
}
}
});
alert.show();
}
我想知道这是否可能
login(nameOfSomeFragment);
顺便说一句,我为Android导航抽屉使用了片段
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_fragment1) {
login(fragmentNameOfThis);
}
else if (id == R.id.nav_fragment2) {
login(fragmentNameOfThis);
}
}
答案 0 :(得分:0)
尝试一下:
// Pass the string tag of the target fragment
private void changeFragment(final String fragTag) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment)
.commit();
}
// Pass the instance of the targeted fragment
private void changeFragment(final Fragment targetFragment) {
// Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, targetFragment)
.commit();
}
// Pass the instance of the target fragment
private void logIn(final Fragment targetFragment) {
//show alert dialog editText and ask for password
alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = String.valueOf(input.getText());
if (value.equals("123456")) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Welcome!", Toast.LENGTH_SHORT).show();
changeFragment(targetFragment);
} else {
x[0]++;
if (x[0] == 5) {
//password entered too many times
} else {
//toast wrong password
}
}
}
});
alert.show();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
// Use switch for better performance
switch (id) {
case R.id.nav_fragment1:
// Passing the instance of FragmentName_1
logIn(FragmentName_1());
break;
case R.id.nav_fragment2:
// Passing the instance of FragmentName_2
logIn(FragmentName_2());
break;
default:
break;
}
}