Check if back key was pressed in android? - java

Say I'm on my main activity and I start a new activity
MainActivity > NewActivity
And from NewActivity I press the back key
MainActivity < NewActivity
I want MainActivity to do something if it's being displayed after NewActivity is closed, but not when MainActivity is run normally, such as when first running the application. Does anyone know if this is possible?

#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
#Update. If you want to be notified when NewActivity is finished, you have to start it by startActivityForResult(Intent, requestCode). Then, you must override onActivityResult() on MainActivity. Check the requestcode parameter here, if the return code equals the submit code (when you start childActivity), put some code to do your business.
int MYCODE=1000;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result OK.d.
if (requestCode == MYCODE) {
// do something good
}
}

I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}

You can override onBackPressed() method in NewActivity which will detect when back button is pressed. And then to inform the MainActivity about it, you can send a boolean flag in a bundle so that MainActivity detects that its opening after NewActivity.
In NewActivity:
#Override
public void onBackPressed() {
boolean fromNewActivity=true;
Intent mainIntent = new Intent(view.getContext(), MainActivity.class);
Bundle bundleObj = new Bundle();
bundleObj.putString("fromNewActivity", Boolean.toString(fromNewActivity));
mainIntent.putExtras(bundleObj);
startActivityForResult(mainIntent, 0);
}
In MainActivity in onCreate() method :
Bundle extras = getIntent().getExtras();
boolean fromNewActivity =Boolean.parseBoolean( extras.getString("fromNewActivity"));
Now you can check if the MainActivity is opened after NewActivity or not.

A couple of ideas:
You can just set a flag in MainActivity when it fires up NewActivity.
You can call startActivityForResult from MainActivity and arrange for NewActivity to set a result, which you will receive in MainActivity.onActivityResult() when NewActivity finishes.

When you start NewActivity you need to use startActivityForResult and use a valid requestId. Such requestId will be passed back to you to onActivityResult once NewActivity finishes.

Related

Android activity doesn't get started from popup menu

I want to open a new activity, Make_a_contact, from my popup menu. The problem I'm sure is related just to the code below, because when I uncomment the code below - Toast.makeText etc...(and remove the code I want fixed) it works fine.
Thanks for any help!
public void Show_Settings(View v) {
//this is the settings button, whose onclick is identified in menu_thisisatest.xml
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_thisisatest, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
Intent intent = new Intent(this, Make_a_contact.class);
return true;
// #Override
// public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(getApplicationContext(),item.toString(),Toast.LENGTH_SHORT).show();
// return true;
// }
});
popup.show();
}
You need to start your new Activity after generating your intent. Try calling
startActivity(intent);
And, of course, you need to include that code in the onMenuItemClick() method, that is currently commented out in your code, i.e.
#Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(this, Make_a_contact.class);
startActivity(intent);
return true;
}
Regarding the comment to #Benjamin Scharbau's answer, when you create intent with new Intent(this, Make_a_contact.class), this is a reference to the anonymous instance of PopupMenu.OnMenuItemClickListener class which isn't inherited from Context (this is the reason for the error). You should use a context in the Intent's constructor, so use the reference to the calling (foreground) Activity, e.g.
Intent intent = new Intent(ClassNameOfCallingActivity.this, Make_a_contact.class)
and pass the intent to the startActivity() method.

how to destroy activity and show fragment?

I have a MainActivity which has a navigation drawer and a framelayout container to show different fragments. On my navigation drawer there is an option which launches another activity(lockpattern activity) to show a lockpattern (had to use activity cause the library doesn't support fragments yet. Link to Library).Once the user has set up his pattern or canceled the procedure,i want the lockpattern activity to get destroyed and show the previous mainActivity and the same fragment what was there in the container before the lockpattern activity was launched.The problem im facing is that once the backbutton is pressed or even if i call the finish() function,it doesn't show the previous activity(ie Main activity) but instead relaunches the lockpattern activity.i have even tried super.onBackPressed(); and it doesn't seem to work.Any help or ideas to get around this is gratefully accepted.
Code of Lockpattern
public class Create_Pattern extends Activity {
private static final int REQ_CREATE_PATTERN = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// This is your preferred flag
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(this);
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getApplicationContext(), "Pattern Recorded",
Toast.LENGTH_LONG).show();
finish();
}
if (resultCode == RESULT_CANCELED) {
finish();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
You should create a new Intent that points to the Activity you want to navigate to next, start that activity by calling startActivity(intent). Then you can call finish() to destroy the current Activity.
Another approach, (which I have not tried yet) is to create an 'up button' hierarchy and manually call NavUtils.navigateUpFromSameTask(this);
Documentation:
http://developer.android.com/training/implementing-navigation/ancestral.html
http://developer.android.com/reference/android/support/v4/app/NavUtils.html

Within Android how do I check if and what other activities have been left open on the stack?

Basically I want to do an if statement like below, but I don't know how.
if (Android_Classes_Left_Open){
Intent a = new Intent(SplashScreen.this, 2ndtoTopElement.class);
startActivity(a);
} else {
Intent a = new Intent(SplashScreen.this, Other.class);
startActivity(a);
}
My SplashScreen code so far:
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, First.class);
startActivity(i);
finish();
}
}, 3000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.splash, menu);
return true;
}
}
The purpose of this is, if the user moves the application into the background they are then returned to their most recently opened activity rather than starting the application from scratch each time. How can I achieve this?
your solution is that whenever you entered in any new activity....
1.) You have to save the name of this activity in shared preferance(on same key) you have to perform this things in onResume() of every activity or your BaseActivity.
2.) At the every time of splash screen you have to check the name of this activity and pass it in your intent.
so you get the last open activity each time after your splash screen done.

Do I need to include a default Intent in order to return to the previous Activity?

I am working on a To Do List Android application (it happens to be for a class assignment, but that's not what I'm asking about--I've tried to leave out as much code as I could). The main screen displays a list of ToDo items with a button at the bottom to open the Add New ToDo Item screen.
On the Add New ToDo Item screen, there is a Cancel button.
Relevant ToDoManagerActivity.java snippet:
public void onCreate(Bundle savedInstanceState) {
// Init and setup adapter, etc.
footerView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ToDoManagerActivity.this, AddToDoActivity.class);
startActivityForResult(intent, ADD_TODO_ITEM_REQUEST);
}
});
// Attach the adapter to this ListActivity's ListView
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
log("Entered onActivityResult()");
// Check result code and request code.
// If user submitted a new ToDoItem
// Create a new ToDoItem from the data Intent
// and then add it to the adapter
}
Relevant AddToDoActivity.java snippet:
protected void onCreate(Bundle savedInstanceState) {
// Initialize default view, handle other events, etc.
final Button cancelButton = (Button) findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setResult(RESULT_CANCELED, new Intent());
finish();
}
});
}
The above code works. Previously, I was trying this in the onClick handler for cancelButton:
public void onClick(View v) {
finishActivity(RESULT_CANCELED);
}
When I clicked the Cancel button, I could see that the onActivityResult was being reached in the logs, but the screen was not reverting back to the main ToDo list screen.
Why does the above code not return me to the previous screen, but the following code does return me to the previous screen? What am I misunderstanding about the task backstack/activities?
public void onClick(View v) {
setResult(RESULT_CANCELED, new Intent());
finish();
}
According to the documentation:
public void finish ()
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via
onActivityResult().
and
public void finishActivity (int requestCode)
Force finish another activity that you had previously started with startActivityForResult(Intent, int).
You should call finish() to close the current activity and finishActivity() to close another activity you started using startActivityForResult(Intent intent, int requestCode). Calling finishActivity() on the current activity will not close it.
Also, there's no point in creating a new Intent for setResult() as you are not passing back any data. Doing this would be sufficient:
setResult(RESULT_CANCELED);
finish();
From Android Docs:
public void finishActivity (int requestCode)
Force finish another activity that you had previously started with startActivityForResult(Intent, int).
finishActivity does not finish the current activity but calls finish for an activity called with requestCode
If you look at the documentation for finishActivity() it says that it will force finish an activity started with startActivityForResult(), but you have to pass in the request code that you used to start the other activity. In your case it would be ADD_TODO_ITEM_REQUEST.
This is probably not the API you want to use. Your 2nd method is cleaner in that you don't need to force close the child activity, but let it finish in the normal way.

Closing app on pressed hardware button back twice returns to a previous activity instead

In my main activity the user gets notified to press the hardware back button to exit the app. This works in most circumstances except for when the user dies. When the user dies it goes to a GameOverActivity. If the user presses the back button in this activity and then proceeds to press back twice on the main activity, it reopens the game over activity. Here is the code, I have declared finish() on the back button in game over activity but it doesn't seem to help.
MainScreen back to quit method:
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
GameOverActivity code:
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class);
startActivity(mainScreenActivityIntent);
finish();
}
});
}
#Override
public void onBackPressed() {
Intent mainScreenActivityIntent = new Intent(GameOverActivity.this, MainScreenActivity.class);
mainScreenActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainScreenActivityIntent);
finish();
}
Here is the logic for the collision and thus creation of GameOverActivity:
if (weight.getBounds().intersect(player.getBounds())) {
player.setTouched(false);
Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
this.getContext().startActivity(gameOverIntent);
((Activity) getContext()).finish();
}
You are starting a new activity and just finishing current activity, add necessary code to finish the activity which you starts from onBackPressed.
I think you have some confuses about Activity life cycle.
Pressing back on GameOverActivity instantiates a new mainScreenActivity, which will produce your problem.
and in your GameOverActivity onbackpressed call super.onBackPressed
#Override
public void onBackPressed()
{
super.onBackPressed();
// and dont start a new activity as you are stacking MainActivity instances
finish()
}
You should call finish() in MainScreen immediately after start Activity GameOverActivity

Categories