I am trying to do a simple task of setting up fragments for my activity to change between them when a menu item is selected.
Is this a proper android design pattern? In iOS I would have separate view controllers for each "Tab", but it seems in Android you are suppose to have 1 activity with many fragments.
Is there example code for creating a simple menu with fragments as thats what I am trying to do.
(I am finding that android documentation is not up to par with iOS)
main_menu.xml
<item
android:id="@+id/need_to_know_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/need_to_know"
/>
<item
android:id="@+id/schedule_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/schedule"
/>
<item
android:id="@+id/news_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/news"
/>
<item
android:id="@+id/map_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/map"
/>
<item
android:id="@+id/tweets_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/tweets"
/>
<item
android:id="@+id/players_menu_item"
android:showAsAction="ifRoom|withText"
android:title="@string/players"
/>
MainActivity.java
package com.example.app;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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, menu);
getActionBar().setHomeButtonEnabled(true);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
case R.id.need_to_know_menu_item:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.schedule_menu_item:
return true;
case R.id.news_menu_item:
return true;
case R.id.map_menu_item:
return true;
case R.id.tweets_menu_item:
return true;
case R.id.players_menu_item:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}