在使用bottomview替换Fragment之前的Web服务调用

时间:2017-09-04 06:04:00

标签: android web-services android-fragments android-activity

我想在 Fragment1 初始化之前调用网络服务。但是在MainActivity首先我的Fragment1替换然后我的网络服务调用发生了。现在该如何处理?我正在使用Retrofit进行Web服务调用。 我无法在片段1中调用Webservice。因为我不想在片段初始化期间重复。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView findViewById(R.id.navigation);

//Here My webservice call which response value sent to Fragment 1 

bottomNavigationView.setOnNavigationItemSelectedListener
    (new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setHomeButtonEnabled(false);
        }

        android.app.Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.action_item1:
                selectedFragment = Fragment1.newInstance();
                toolbar.setTitle("1");
                break;
            case R.id.action_item2:
                selectedFragment = Fragment2.newInstance();
                toolbar.setTitle("2");
                break;

            default:
                selectedFragment = Fragment1.newInstance();
                toolbar.setTitle("1");
                break;

        }
        android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, selectedFragment);
        transaction.commit();
        return true;
        }
    });


android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, Fragment1.newInstance());
transaction.commit();
    } 
}

0 个答案:

没有答案