Android action bar navigation - android

I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};

There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity

Related

How to prevent for recreate activity when navigation drawer menu click

In my application i don't want to recreate activity on navigation drawer menu click when i am on same activity.
user on map activity after navigation drawer item click map reloading. i don't want to reload map or refresh activity.
private void displayView(int position)
{
switch (position)
{
case 0:
break;
case 1:
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
break;
case 2:
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
break;
case 3:
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
break;
default:
break;
}
You can add the following to your function
private int current=-1;
private void displayView(int position)
{
switch (position)
{
case 0:
current =0;
break;
case 1:
if(current!=1){
current =1;
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
}
break;
case 2:
if(current!=2){
current=2;
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
}
break;
case 3:
if(current!=3){
current=3
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
}
break;
default:
break;
}

Send float from activity to activity

I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !
Here is my code from the main :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the StatsActivity.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_main);
Intent intent = getIntent();
float consumOn = intent.getFloatExtra("consumOn", 0);
float consumOff = intent.getFloatExtra("consumOff", 0);
EditText editTextA = (EditText)findViewById(R.id.editText1);
editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);
EditText editTextB = (EditText)findViewById(R.id.editText2);
editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
}
When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.
Replace the getFloatExtra with
Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");
I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
with
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
because you haven't actually defined their type anywhere and they aren't variables.
You should pass bundle with your floats to intent and only then extract them
I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_about:
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Displaying error

I have encountered a problem. When I'm at this page (the main menu) and I try to click the calculate button it will go back to the login page. However, the calculate and records page are all also opened, and can be accessed by clicking the back button.
Button Calculate;
Button Records;
Button Logout;
Calculate = (Button) findViewById(R.id.buttonCalculate);
Records =(Button) findViewById(R.id.buttonRecords);
Logout = (Button) findViewById(R.id.buttonLogout);
Calculate.setOnClickListener(this);
Records.setOnClickListener(this);
Logout.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
public void onClick(View view)
{
Intent i = new Intent(this,Calculate.class);
startActivity(i);
Intent f = new Intent(this,Records.class);
startActivity(f);
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
}
}
Try this way:
public void onClick(View v)
{
switch(v.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
default:
break;
}
}
and make sure you have to added all the Activities in your manifest.xml file
try this..
public void onClick(View view)
{
Switch(view.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
}
}

onListItemClick within onDialogPositiveClick?

How can I refer to onListItemClick within onDialogPositiveClick? So the page does not open before/while the dialog box opens..Can I literally put onListItemClick in onDialogPositiveClick, or do I have to do something completely different? Here is my code...thanks for all/any help!
public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] sites = {"Google", "Amazon", "Ebay" , "Reddit", "SmashingMag", "CCC"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, R.id.textView1, sites);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
DialogFragment newFragment = new TheDialog();
newFragment.show(getFragmentManager(), "Confirm");
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}
}
#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);
return true;
}
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
}
What you can do is not fire intent on list item click and move the switch code to
onDialogPositiveClick(DialogFragment dialog){
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}}
This way user will only go to the next page when positive button is clicked, hope you want this kind of behavior.

Android Buttons open up the wrong activities

I have three buttons on my main page. Something weird happens when I try to click on them. For example, when I click on the NewGame button, it first displays what the scores button should display, and then if I click the back button it will proceed to display the activity that it was meant to. With the About button, I have to click back twice (it displays the newGame activity and the scores activity. Is there a reason why this is happening?
public class Sakurame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//set up click listeners for buttons
View HighScoreButton = findViewById(R.id.highscore_button);
HighScoreButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any)
}
return false;
}
public void onClick(View v){
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
//break;
// more buttons go here (if any)
}
}
Try adding break; after each startActivity within the onClick method.
Edit to clarify. This ensures that once the case has been met, the switch statement is broken from instead of moving on to the next case statement.
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
break;
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
break;

Categories

Resources