0
votes

I am trying to fix up my app code and transition from Activities to Fragments, in order to transition to the navigation draw and eventually have some sliding tabs too.

Currently for navigation I am using the 3 dots drop down menu from the action bar which I know is not really right. Which is why I am trying to transition to fragments.

I want to keep my action bar menu so I can keep my search icon in the action bar but I want to add a navigation draw to it.

I currentley have a class called ActionBarMenu which extends activity. All my activities come form this because this class holds all the functions which open new activities for the action bar menu I have set up right now.

public class ActionbarMenu extends Activity {

    public void goToSearch (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,Search.class);
        startActivity(i); 
    }

    public void goToStatistics (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,StatisticsPage.class);
        startActivity(i); 
    }

    public void goToFindBrewery (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,FindBrewery.class);
        startActivity(i);
    }

    public void goToContact (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,ContactPage.class);
        startActivity(i);
    }

    public void goToPortfolio (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,Portfolio.class);
        startActivity(i);
    }

    public void goToDiscover (MenuItem item) {
        // go to search page
        Intent i = new Intent(this,Discover.class);
        startActivity(i);
    }

    public void scanBarcode (MenuItem item) {
        //open scanner
        IntentIntegrator scanIntegrator = new IntentIntegrator(this);
        scanIntegrator.initiateScan();
    }

    //get result value
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        // retrieve scan result
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            // we have a result
            String scanContent = scanningResult.getContents();
            // launch async task for results
            String url = "http://api.brewerydb.com/v2/search/upc?code=" + scanContent + "&key=0cdb22f84315834cc807c0176a927060&format=json&withBreweries=y";

            new GetBeerDataFromUPCJSON(this).execute(url);
        } else {
            Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

I tried to change it to a fragment by changing extends activity to extends Fragment and all intents I build to launch new activities all get the red squiggly underline. I am just experimenting and trying to learn what's going on and whats the best way to transition my app from activities to Fragments, without having to recode everything.

5

5 Answers

2
votes

Your problem is probably because in a line such as

Intent i = new Intent(this, Discover.class);

this now refers to a Fragment instead of an Activity. Intent() expects a Context as the first argument; Activity extends Context, but Fragment does not. To fix your problem, you should change this to getActivity().

For future reference, mousing over the little red x that appears next to the error should tell you exactly what the problem is.

0
votes

Instead of using intents, you'll use the fragment manager to load new fragments. Read this page on the android website about fragments.

0
votes

The finished code for quick replacement would be:

Intent i = new Intent(getActivity(), Discover.class);
startActivity(i);
0
votes

you just need to make it same like backpressed if the activity is just next from your fragment like... it should work ....

        @Override
        public void onClick(View view) {

     onBackPressed();

        }
-2
votes

Intent i = new Intent(this, Discover.class);