Jeffer1980
Jeffer1980

Reputation: 13

OnCreate method is not called (Sunshine app)

I'm working on the android self study Sunshine app and I'm having trouble getting extra menu entries in the action bar.

The most likely reason I've found, is that the OnCreate method of my frame class is not being called and thus the menu is not being inflated.

Here's my MainActivity code:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("MainActivity","OnCreate Started 1");
    if (savedInstanceState == null) {
        Log.d("MainActivity","OnCreate Started 2");
        ForecastFragment MyFragment = new ForecastFragment();
        if (MyFragment == null){
            Log.d("MainActivity","OnCreate Started 3");
        }
        else{
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, MyFragment)
                .commit();}
    }
}

Here is now the code of my ForecastFragment class:

public class ForecastFragment extends Fragment {

public ForecastFragment() {
}

//@Override
protected void OnCreate(Bundle savedInstanceState){
    Log.d("Forecastfragment","OnCreate Started");
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.

    inflater.inflate(R.menu.forecastfragment, menu);

}

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_refresh) {
        FetchWeatherTask WeatherTask = new FetchWeatherTask();
        WeatherTask.execute();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

When I run the app, I don't see the logging that the OnCreate method of the ForecastFragment class is being called.

Any ideas?

Upvotes: 0

Views: 44

Answers (1)

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

This is not the exact inherited onCreate method of Fragment

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
    }

it should be onCreate not OnCreate

Upvotes: 2

Related Questions