嗨在我的应用程序中有片段通过接口传递一个值传递给另一个片段,但在我的主片段中声明第二个片段时它显示为空。
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,Receive {
private static final int INTRO_SCREEN = 200;
MainScreen mainScreen;
private static final String TAG = MainScreen.class.getName();
private MyAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
Log.d(TAG,"isFirstTime");
startDefaultIntro();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
mPagerAdapter = new MyAdapter(fm);
viewPager.setAdapter(mPagerAdapter);
}
private void showAlertDialog(){
InputFragment ip = new InputFragment();
ip.show(getSupportFragmentManager(),"inp");
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDataReceive(double result) {
mainScreen.getData(result); //here its the transferring data to other fragment but its showing mainScreen null
}
private class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i == 0){
fragment = new MainScreen();
}
if (i == 1){
fragment = new Consumption();
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
}
public void startDefaultIntro(){
Intent intent = new Intent(this, MyIntro.class);
startActivityForResult(intent,INTRO_SCREEN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG,"onActivityResult() resultCode:"+resultCode+" requestCode:"+requestCode);
if(resultCode == RESULT_OK){
showAlertDialog();
}
}
}