Elvir Ibrahimov Elvir Ibrahimov - 2 months ago 16
Java Question

I cant call method Fragment in Activity

I have

TwoFragment
inside method



public class TwoFragment extends Fragment{
Button btnweb;
public WebView mWebView;

public TwoFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v=inflater.inflate(R.layout.fragment_two, container, false);
btnweb = (Button) v.findViewById(R.id.btnweb) ;
btnweb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnweb:
mWebView.reload();
break;
}
}
});


mWebView = (WebView) v.findViewById(R.id.webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

//metodi optimizacii


mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT >= 19) {
// chromium, enable hardware acceleration
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

}
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);


// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());

return v;
}

public void reloadWebView() {
mWebView.loadUrl("javascript:window.location.reload(true)");
}





I tried call method in
MainActivity
but crashed..

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_progress_show) {
progress_menu_item.setActionView(R.layout.menu_item_layout);

//how i can call method here ??

return true;




Its my Main Activity FUll



public class MainActivity extends AppCompatActivity {
Menu mymenu;
MenuItem progress_menu_item;
private int[] tabIcons = {
R.drawable.ic_home_white_24dp,
R.drawable.ic_3d_rotation_white_24dp,
R.drawable.ic_location_on_white_24dp

};

private Toolbar toolbar;
private TabLayout tabLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

CustomViewPager viewPager = (CustomViewPager ) findViewById(R.id.viewpager);
viewPager.setPagingEnabled(false);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();




}

private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ƏSAS");
adapter.addFragment(new TwoFragment(), "VIRTUAL TUR");
adapter.addFragment(new ThreeFragment(), "ÜNVANIMIZ");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);

mymenu = menu;
progress_menu_item = mymenu.findItem(R.id.action_progress_show);


return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_progress_show) {
progress_menu_item.setActionView(R.layout.menu_item_layout);

//HERE NEEd TO CALL IN METHOd fRAGMENT FOR REFRESH WEBVIEW ...

return true;
}

return super.onOptionsItemSelected(item);
}




Answer

Try this:

Create object for fragments;

OneFragment frag1;
TwoFragment frag2;
ThreeFragment frag3;
CustomViewPager viewPager;

In onCreate() :

@Override
    protected void onCreate(Bundle savedInstanceState) {

        //------- CHANGE BELOW LINE--------

        viewPager = (CustomViewPager ) findViewById(R.id.viewpager);

        //-------
    }

In setupViewPager method :

private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        frag1=new OneFragment();
        frag2=new TwoFragment();
        frag3=new ThreeFragment();

        adapter.addFragment(frag1, "ƏSAS");
        adapter.addFragment(frag2, "VIRTUAL TUR");
        adapter.addFragment(frag3, "ÜNVANIMIZ");
        viewPager.setAdapter(adapter);
    }

In onOptionsItemSelected :

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_progress_show) {
            progress_menu_item.setActionView(R.layout.menu_item_layout);

            if(viewPager.getCurrentItem()==1)
            {
                frag2.reloadWebView(); // Call method
            }

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
Comments