Navigation inside tab in android - android

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); }

Related

Navigating between activities

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;
}

Back button stuck in endless loop?

My app is currently stuck in an endless loop with the physical back button. The program is set up to load a splash screen and then transition to a main menu. Once at the main menu the user can switch to another activity of their choice. For example: The new game activity. Once the user is at the new game activity I want them to be able to hit the back button and have it take them to the main menu. Once back at the main menu, if they hit the back button again I would like it to exit the game.
This is what I am using for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
}
return super.onKeyDown(keyCode, event);
}
It works properly and takes the user back to the main menu without a problem. But if the user hits the back button again once at the main menu it will take them to the screen they just escaped from. So it just loops back to the previous screen every time.
This is how the main menu is set up:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
If I press the back button as soon as the main menu loads the first time it will work properly and close the game. It only messes up if I've hit the back button previously to get from one screen to the main menu.
Update:
Okay that kind of worked. When I hit the back button at the main screen the music stops and it acts like it's trying to close the app but it flashes back to the main screen again. I have to hit it twice every time.
You should call finish() inside the onKeyDown method of your activities so your current activity is actually removed from the stack.
Something like this should work:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
finish();
}
return super.onKeyDown(keyCode, event);
}
First (from comments) add return true; within the if statements (to indicate that you have handled the event), like so:
// Main menu code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
return true; // Add me!
}
return super.onKeyDown(keyCode, event);
}
Additionally, I believe because you're "starting" the menu activity again, you have to clear the back stack (the order of activities used so far) before opening it. You should be able to achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or maybe Intent.FLAG_ACTIVITY_CLEAR_TASK). Something like this:
// Other activity code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me!
startActivity(menuIntent);
return true; // Add me, too!
}
return super.onKeyDown(keyCode, event);
}
You haven't called finish() in your other activities so when you call finish() on the menu it just closes itself and the other activity is exposed

Android: Override onKeyUp and onKeyDown in Intent.ACTION_VIEW

I have an Android tabbed application that displays music charts (say, ChartActivity), each of which in turn displays a list of music tunes (say, EntryActivity). Tapping on each entry of music tunes will launch, e.g., a YouTube app (default actions). That is, in list.setOnItemClickListener of EntryActivity, I make calls:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(selectedSongUrl));
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("YouTube", intent);
(Yes, I use ActivityGroup and TabActivity.)
The problem is that when I tab the back button from the YouTube screen, the active view does not come back to EntryActivity, but to ChartActivity. I guess, this is because I could not override onKeyUp and onKeyDown in YouTube screen so that I can further pass the event to the parent activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
// Overrides the default implementation for KeyEvent.KEYCODE_BACK
// so that all systems call onBackPressed().
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
Is there any way to let EntryActivity know callback events sent from Intent.ACTION_VIEW?
I'm a little confused on the setup of your app... Is this a traditional tabbed activity where each tab displays its own activity (e.g. ChartActivity and EntryActivity are tabs)?
If so, you could do the following in your TabGroupActivity:
Override onPause() to save the index of the currently active tab in SharedPreferences.
Override onResume() to read in that preference (if it exists) and switch to the tab that you were last in.
Override onDestroy() and remove that preference so the next time you launch the activity you start in the correct tab.
If that is not how your activity is set up, please explain a little more about how things are set up.

While using ViewFlipper is it possible to map the back button to go to the previous View rather than back out of the entire application?

My application is set up using ViewFlipper so that I have one main View which links to three other Views, each through their own button. As I've set it up this way, when I'm on one of the three other Views if I press the back button on my android device it backs all the way out of the app. I Just wanted to know if it was possible to map it to just go back to the main View?
Override onKeyDown() :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Place your code here!
}
return true;
}

Problem in using keyboard's Back button in Android

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

Categories

Resources