I started programming for android with eclipse a few days ago
However, I am stuck now.
I would like to add a new activity, so that i can add a new screen with new layout (an info screen about the app)
It is made so that when you press a specific menu button (help) it launches the help.xml activity and shows a new screen with some new words.
This succeeded one time, but I cant manage to do this another time.
Here is the code to direct to the activity, made from the main.java:
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate (R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
case R.id.quit:
System.exit(0);
break;
I dont have code in the help.java, and I have made some buttons in help.xml
The 'quit' button works fine but the help thing doesnt.
I am also very unsure where it links to, as the previous time I had to both make a help.java and a help.xml activity.
I already noted the help activity in the manifest.xml
Help would be appreciated, thanks in advance!
You forgot to write break after starting new Activity. That was the issue.
Replace this code:
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
case R.id.quit:
System.exit(0);
break;
with
case R.id.help:
Intent intent = new Intent(this, help.class);
startActivity(intent);
break;
case R.id.quit:
System.exit(0);
break;
you forgot the break, and then in your help class
public class help extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
}
}
Related
I have made an app with many activities and I have included a bottom navigation bar to all of them. I used finish(); after any intent not to get any memory leak problem but when I press the back button of my phone the app closes. What should I do?
//an intent of my app
Button btnsuita = (Button) findViewById(R.id.souita_btn);
btnsuita.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HotelRooms.this, HotelRoomsSouita.class);
startActivity(intent);
finish();
}
});
//An Example of Bottom Navigation Bar
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.BottomNavView_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
android.view.MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_home:
Intent intent1 = new Intent(HotelRoomsSouita.this, MainMenu.class);
startActivity(intent1);
finish();
break;
case R.id.ic_back:
Intent intent2 = new Intent(HotelRoomsSouita.this, HotelRooms.class);
startActivity(intent2);
finish();
break;
case R.id.ic_contact_us:
Intent intent3 = new Intent(HotelRoomsSouita.this, Mail.class);
startActivity(intent3);
finish();
break;
}
return false;
}
});
Remove finish() in the switch every case.
If you want to ignore back button clicks, then you can add this code in the activity.
#Override
public void onBackPressed() {
//Do anything if you wish
}
Since you are finishing every activity ,there is o activity remain in the backstack or background . so the application closes.
You can use view pager to inflate fragment for bottom navigation view . This is the correct approach by google material design guid lines.
Never use activity for bottom navigation view .
And still if you continue with your activity , then override the onBackpress method of the activtiy .
If I understand your problem correctly you want the user to be able to go back in previous activities while your application does not cause any memory leaks(by starting same activity again and again)
Solution:
Remove finish() from each case in onNavigationItemSelected
Go to your manifest file and add android:launchMode="singleInstance" to each Activity
Ovveride onBackPressed so that it does nothing in your first-started activity as dcanh121 said.
If you do that then if user tries to open the same Activity which was already opened before, it will cause the app to open the previous activity instead of creating a new one.. (read more about this)
Therefore you won't have any memory leak problems :)
Im able to send data to parent activity when back button is pressed.
However, for up button i cannot implement similar logic and data is not getting returned to parent activity why is that ?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
.....
Intent resultIntent = new Intent();
resultIntent.putExtra("noteJSON", noteJSON.toString());
setResult(Activity.RESULT_OK, resultIntent);
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Im using startActivityForResult to start childactivity. On onactivityresult basically handling the intent info. When back, button is pressed its working. When UP is pressed onactivityresult never gets executed. Thats the problem
When you call navigateUpFromSameTask method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. As a result onActivityResult won't be called.
If you want to ensure result is returned via onActivityResult then remove call to navigateUpFromSameTask as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
....
Intent resultIntent = new Intent();
resultIntent.putExtra("noteJSON", noteJSON.toString());
setResult(Activity.RESULT_OK, resultIntent);
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
try below
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
.....
startActivity(new Intent(YourActivity.this,ParentActivity.class).putExtra("noteJSON", noteJSON.toString()));
return true;
The code worked back when I still used an EditText, but I changed it to an AutoCompleteTextView to make things easier for the user, and now I have a problem. Here is the bulk of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
String[] items = getResources().getStringArray(R.array.items_array);
java.util.Arrays.sort(items);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
edit.setAdapter(adapter);
public void find(View view) {
String name = edit.getText().toString();
if(name.equalsIgnoreCase("Brown Turkey Fig")){
Intent intent = new Intent(this, BrownTurkeyFig.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Emerald Green Arborvitae")){
Intent intent = new Intent(this, EmeraldGreenArborvitae.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Delaware Valley White Azalea")){
Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class);
startActivity(intent);
}
Ive included the parts that are related to the question. Basically, the user types the name of a plant in the AutoCompleteTextView, and then clicks a button to be brought to a new activity based on whatever plant they typed in (I included three as examples at the end). Before I added the autofill part, the code worked fine the way it was.
The problem is that when the button is clicked, the next activity is not brought up. It crashes the app.
I do not know what is wrong with it now. Perhaps I am not properly taking the actual text from the AutoCompleteTextView to be compared in my if statements? Any help is appreciated!
You are declaring a local variable "edit" in your oncreate method but also you are using a global variable in your "find" method.
I'm sorry , I can't understand what's your meaning. Maybe you can try to use AutoCompleteTextView.setOnItemClickListener. For example:
edit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = items.get(position);
if(item.equalsIgnoreCase("Brown Turkey Fig")){
Intent intent = new Intent(this, BrownTurkeyFig.class);
startActivity(intent);
} else if(item.equalsIgnoreCase("Emerald GreenArborvitae"){
Intent intent = new
Intent(this,EmeraldGreenArborvitae.class);
startActivity(intent);
} else if(item.equalsIgnoreCase("Delaware Valley White Azalea")){
Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class);
startActivity(intent);
}
}
})
Hope to help you.
Please help me.
I use onOptionsItemSelected to get to settings layout. There I activate setDisplayHomeAsUpEnabled
public boolean onOptionsItemSelected(MenuItem settings){
setContentView(R.layout.settings);
getActionBar().setDisplayHomeAsUpEnabled(true);
return super.onOptionsItemSelected(settings);
}
How can I get back to main layout from settings layout?
In onOptionsItemSelected you need to find out whether the MenuItem that is clicked is your Home item.
I would use a switch like:
switch (settings.getItemId()) {
// If home icon is clicked return to main Activity
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
Then I would either start the MainActivity again or just call finish() to end the current one. There is a very nice article with loads of samples which you might find useful here: http://www.vogella.com/articles/AndroidActionBar/article.html
So, I have a menu activity and there a user can choose a game to play. When he does that, in game he has an option to go back to menu (either on backPress or on my Exit button in game). I need, when that happens, to disable that game button, so the user cannot play it again. How can I do that?
I tried to make an object from Menu class and then disable the button but get error. Also tried:
public void disableButton(){
button.setEnabled(false);
}
and then calling it:
obj.disableButton
But I get stactOverflow errors on line where I created my obj as soon my menu activity starts. How to do this?
EDIT:
In Menu class:
public void disableButton(){
button.setEnabled(false);
}
In Game class:
Menu objMenu = new Menu();
And then in exit onClickListener:
objMenu.disableButton();
EDIT2:
Here's how I call my game activity:
Intent i = new Intent(this, Game.class);
startActivityForResult(i, GAME);
Then in game activity:
Intent resp = new Intent();
resp.putExtra("score", numberOfPoints);
setResult(1, resp);
finish();
And again in Menu class:
final private static int AS = 1;
#Override
protected void onActivityResult(int reqCode, int respCode, Intent i) {
if(respCode == 1) {
switch(reqCode) {
case AS: receivedA = i.getIntExtra("score", receivedA);
button.setEnabled(false);
break;
}
So, this is what I do when game ends, and I disable that button succesfully.
I think the issues is not only in button.setEnabled(false);. As it is hard to say what is the problem without seeing code's details, I am suggesting another solution
start the game activity from menu activity using startActivityForResult
when the game is finished ,in the return intent put a specific data
In Menu activity, in onActivityResult check if the result intent has that specific data
basing on step 3 you can enable or disable the button