Cannot display Toast from an Activity other than my main activity - java

I have a Activity called main. If I call
Toast.makeText(this, "Hello World from main", Toast.LENGTH_SHORT);
this works fine. However, for every other activity in my application, I cannot display a Toast. No exception, nothing in the Log but I don't see the Toast.
My main activity starts another one with an options menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.main_menu_entry:
Intent infolist = new Intent(this, infolist.class);
startActivityForResult(infolist, R.layout.infolist);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In my infolist activity I have another options menu which should display a Toast.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.infolist_menu_entry:
// this Toast is never shown.
Toast.makeText(this, "Hello World from infolist", Toast.LENGTH_Short);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Any ideas what could cause this issue?
I am using the latest SDK with Min SDK Version = 3 and an 1.5 Emulator.

I would say, classical error :
You forgot the Toast.show() method ;)

You miss the show() method at the end.
Toast.makeText(this, "Hello World from infolist", Toast.LENGTH_Short).show();

Related

Settings Back Button Isn't Working Using Bellow Code?

Below link in settings activity cannot go back when back button is pressed.
https://github.com/SadaqaWorks/Word-By-Word-Quran-Android/blob/master/app/src/main/java/com/sadaqaworks/quranprojects/activity/SettingsActivity.java
Code is given below...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)`enter code here`
}
return super.onOptionsItemSelected(item);
}
nothing gets work. plz fix the code.
You may want to use finish() instead of System.exit(0).
And do not override onBackPressed().

Action Bar Launch-Intent Behaviour with Multiple Menu Items

I am making a simple application that allows the user to take a photo using the dispatchTakePictureIntent() that saves the full-size photo; indicated here; http://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent. And launched using a Camera icon from the action bar. The user then takes a photo presses ok and their picture appears in a list created using a class extending BaseAdapter
public boolean onOptionsItemSelected(MenuItem item) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
return super.onOptionsItemSelected(item);
}
If I launch the intent as above then everything seems to work fine but I have no response to the delete all button (clicking it would ofc just start the camera intent)
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_photo : {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
}
case (R.id.action_delete) : {
deleteAllPhotos();
}
}
return super.onOptionsItemSelected(item);
}
However when setup like this I can delete the photos, but the previously taken photo dissapears every time I click the camera icon to take a new photo
Is there any subtle change in lifecycle or anything introduced by this difference? As I am struggling to find how it can cause this different behaviour.
Thanks for any input!
You're missing a break or return statement after your case, so it launches the intent and then deletes all of your previous photos since case statements fall thru to the next one if not told to break.
Try:
switch(item.getItemId()) {
case R.id.action_photo:
...
break;
case R.id.action_delete:
...
break;
}

Back button in header "refresh" the previus activity

I'm new to Android and I trying to do a simple app.
In the "home", I write my name and press the "send" button.
In the second page, the app show "Hello " + inserted name.
The problem is:
When I touch the back virtual button in nav bar, I come back to the home and in the input field there is always the name; but when I go back with the back button in header bar, the input field is empty.
I've tried with:
in onOptionsItemSelected:
switch (item.getItemId()){
case android.R.id.home:
Log.d("case","go back");
NavUtils.navigateUpFromSameTask(this); //or with finish();
return true;
}
it prints "case: go back" in console, but doesnt' work correcly.
My custom theme have as parent Theme.AppCompat.Light.DarkActionBar and if i put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreate, Android Studio tells me "Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'".
I solved adding this in the second Activity:
#Override
public Intent getSupportParentActivityIntent() {
return null;
}
and edit the switch/case function like this:
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return false;
default:
return super.onOptionsItemSelected(item);
}

Android tutorial switch 1st case method is unreachable

I am doing the android tutorial and in the Adding Actions Buttons part, openSearch() and openSettings() are undefined. So I made them as private voids in the same class. In the switch, though, openSearch(); is apparently unreachable. When I delete that case, the method in the next case is unreachable. Here is my code.
return super.onOptionsItemSelected(item);
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
private void openSettings() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
private void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
You should get rid of return super.onOptionsItemSelected(item); in the top of your method, or the switch will be never reachable

how to override action bar back button in android?

I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#Override
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();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...

Categories