我必须在我的主要活动上包含三个片段,点击应用栏上的三个按钮。现在,问题是我想从第二个活动返回到mainActivity上的特定片段我正在使用Intent但是在app intent开始时为null并且获得 nullpointer异常和应用程序崩溃。
这是 MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
FragmentTransaction fragmentTransaction;
HomeFragment homeFrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
//--------Set and Get Tool Bar-------//
setSupportActionBar(binding.appbar);
getSupportActionBar().setTitle("");
if(getIntent() !=null){
String is = getIntent().getStringExtra("check");
if(is.equals("1")){
SearchFragment searchFrag = new SearchFragment();
if(searchFrag != null){
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, searchFrag)
.addToBackStack(null).commit();
}
}
}else{
homeFrag = new HomeFragment();
if(homeFrag!=null) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, homeFrag).commit();
}
}
}
public void MoveToSearchActivity(View view){
SearchFragment searchFag = new SearchFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,searchFag).commit();
}
public void MoveToHomeActivity(View view){
if(homeFrag!=null) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, homeFrag).commit();
}
}
public void MoveToAlertFrag(View view){
AlertFragment alertFragment = new AlertFragment();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,alertFragment).commit();
}
这是我的 FilterActivity.java ,我将从中回到片段:
public class FilterActivity extends AppCompatActivity{
private ActivityFilterBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
//--------------Get ToolBar-----------//
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(" ");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent homeIntent = new Intent(FilterActivity.this, MainActivity.class);
homeIntent.putExtra("check",1);
startActivity(homeIntent);
break;
default:
break;
}
return true;
}
}
感谢。
答案 0 :(得分:0)
在MainActivity
,
// Toolbar code
...
int is = -1;
if (getIntent() != null && getIntent().hasExtra("check")) {
is = getIntent().getIntExtra("check", -1);
}
switch (is) {
case 1:
// load SearchFragment
break;
case 2:
// load MoveToAlertFrag
break;
default:
// load HomeFragment
break;
}