I'm developing an app. for my semester project.App. is simple true&false game.I did something about it but i choked up one point.My problem is that handling activities.
Here is the quick view of the design.
http://imgur.com/2Fhsrn8
I created all activities and my codes working correct.But there is an issue.When i'm on third or fourth page(activity) and press back button on my phone it returns the second page then i automatically have another chance to answer question.All i want is turn the first screen when back button is pressed and clear all the data such as assign score as 0.
I'will be grateful for any kind of help.
Use
finish();
for your current activity when you start the new intent(e.g. when going from activity 3 to activity on your picture) and add this to your activity's 2-3 if you want to start again when you click back, otherwise it will close your app.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//implement code to start 1st activity here!
}
return super.onKeyDown(keyCode, event);
}
you should override onBackPressed on your third, and fourth activity ->
#Override
public void onBackPressed() {
// here you should create intent to your firstActivity
// and assign variables -> e.g.
// score = 0;
}
Related
In my app..there are 2 activities i.e.
page1 and page2.
flow is page1-->page2
On page1, i have used many checkboxes. After selection of one checkbox, when i presses the "next" button, it goes to page2 activity showing the result but when i presses back button and select another checkbox, it is still showing the same result on page2 i.e. of previous selection.
I m new to android.please help me out.Thanks in advance.
i thik you should mentain page1 using
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// here you checkyour checked checkbox items and make it unchecked
}
this mayhelps you
I thought you were having your own back button..if you're using the phone's back button, try this code to finish the current activity and go to the previous activity [this is to be added to page2 activity]:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
If you want to implement your own back button, under the listener for the button, use an intent to start an activity to go back to the previous activity..
Intent intent = new Intent(Page2ActivityFileName.this, Page1ActivityFileName.class);
startActivity(intent);
finish();
See you're probably using startactivity(intent); whereas in this case you should use startactivityforresult(intent); Hope you get it, if not drop a comment I'll elaborate my answer with some code.
I have a tab activity, which contains 4 tabs and each one contain activities. In the fourth tab i have got a list activity. it will list a number of options. when we click and option it will go to another activity. and when i press back button from that activity the application exits. But actually i want to go back to the list of options.
Can any body help me to get rid of this
Regards
Pramod
Override the back button with this code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Enter code here telling it what you want to do when you hit back
return true;
}
return super.onKeyDown(keyCode, event);
}
Hope that helps!!
Override the onPause() of the Activity you mentioned above "when we click and option it will go to another activity" to go to the Activity of the Tab containing the options via Intent with its class.
Use getParent() to the activity & use the below to so that you will be able add up view by clearing up existing views & adding new views to the top.
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
All the tabs reside inside the activity, so when the backkey has been pressed the complete activity goes into background, so if you want to navigate to a particular activity override the onkeydown method and navigate to the particular tab
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MyActivity mMyActivity;
mMyActivity = (MyActivity) this.getParent();
mMyActivity.switchTab(tabnumber);
return true;
}
return super.onKeyDown(keyCode, event); }
I am developing an application using TabHost. I am using android default back button to move back to previous activity from current activity by overriding onBackPressed() method inside ActivityGroup of each tab.
Now , the problem is , in one of my activity i have an EditText which get focused when the activity starts. Then , if i press back , it does not go to previous activity , instead it closes the application. By searching about the problem on internet what i have found is that when the EditText get focused which is a child view of activity's view , the activity loss focus and then if back button is pressed , for lack of focus on the current activity it closes the application. Still i am little bit confused or can be say not clear about the problem.
So, any how , i have managed to set and remove focus over EditText on run time using code. But still now , as the EditText does not have focus , if the back button is pressed , it closes the application. I am really confused what's actually going on. So , if anyone have any idea or solution about the problem , please help on the issue. I will appreciate that very much. Thanks.
You can override this behavior by adding Key Listener to your EditText. Try this out,
name_edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.i("Back event Trigered", "Back event");
activitygroup.back();
}
return false;
}
});
try this..
#Override
public void onBackPressed() {
onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
super.onBackPressed();
}
try this
public void onBackPressed() {
startActivity(new Intent(currentActivity.this, previousActivity.class));
finish();
}
I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity
I am using following code to start activity when user pressing search button on the handset
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Util.startActivity(ReviewsDetail.this, KeywordSearch.class);
return false;
}else{
return super.onKeyUp(keyCode, event);
}
}
But here are few issues with it please look at the following image.
When press search button it first show google search box at the top of activity then start activity which i want to start
When click on the back button displays empty actiivty
#Override
public boolean onSearchRequested() {
// your logic here
return false; // don't go ahead and show the search box
}
The Search button and system's search request are both working the same when invoked from any activity of your app. If you want to override it you will have to override it for EVERY activity you want it to work from in the same way. Unfortunately, there is no way to override it "globally", neither a way to subclass/style/theme the default search popup. So sad, google.