0

I have an app in which there is a gridview of small images and when you click on each gridview item, it opens a full screen version of the image in a new activity with button on it left and right to go to previous/next image which are also on another activity. Now, if the user touches back button on any of the fullscreen image avtivites, i want it to go to the activity with the gridview, instead, it does an unwinding and goes to the previous image and retraces the steps that the user has performed.

How do i make the back button redirect to gridview activity. I would prefer to use activities for the images, rather than fragments?

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63
  • What are you doing when the user clicks on the left or right button to show the next/previous image? – Mus Jul 09 '13 at 07:07
  • Post some code which makes others to understand your issue rather than description. – GrIsHu Jul 09 '13 at 07:15
  • i am using intent to navigate between the activities which show the full screen images – Shivam Bhalla Jul 09 '13 at 07:33
  • ok. i seem to have found a way by using onkeydown() function to launch an intent into the previous activity by putting the keycode as KEYCODE_BACK – Shivam Bhalla Jul 09 '13 at 07:39

1 Answers1

0

You can use the right method for that:

    @Override
    public void onBackPressed() {
         super.onBackPressed();
         Intent intent = new Intent(this, YOUR_ACTIVITY);
         startActivity(intent);
         finish();
    }
red_alert
  • 1,738
  • 14
  • 24
  • Thanks. This works somewhat. The basic of what i am trying is a flow like this ACT1->ACT2->ACT3->ACT4. So using your solution in ACT4 takes me to ACT1. Thats fine. But then if i again touch back, it takes me to ACT3->ACT2 and then finally ACT1 again. I want that after touching back on ACT1, the back button should return to its normal functionality of going behind by one activity. – Shivam Bhalla Jul 09 '13 at 08:28
  • If you want the default functionality of back button in one activity only have to place this code in the activity: @Override public void onBackPressed() { super.onBackPressed(); } – red_alert Jul 09 '13 at 08:33
  • When you do ACT3->ACT4 you are finishing ACT3, for example? Maybe that is your problem... – red_alert Jul 09 '13 at 08:41
  • no i have used finish() only while using back button as in your code.Not when going from act3->act4. here i have only used a simple intent – Shivam Bhalla Jul 09 '13 at 08:56
  • And in your manifest you have declared parent activities? Also you can (onBackPressed) if you are in ACT4 finish(ACT4) and start ACT3, for example... – red_alert Jul 09 '13 at 08:57
  • it is like implementing a quiz. I have an introduction screen first. Then when the user clicks on 'start', we go to first question, then second, then third. Now when he clicks on back button on any of the levels, instead of going to previous level, he should go to introduction screen. And now, if he touches back, he should go to whatever was present before the quiz. – Shivam Bhalla Jul 09 '13 at 08:59
  • Maybe I'm not understanding the problem, but I think that you can use the fist code that I show. If you're in ACT4, onBackPressed you can control which activity are showed (ACT3 or the start activity or other that you whant) – red_alert Jul 09 '13 at 09:04
  • yes. like a said. that part of the problem is solved. now however, once i have reached act1 from act4, i would like to leave the app/go to any previous activity when i press the back button again. That is not happening – Shivam Bhalla Jul 09 '13 at 09:33
  • In act1, if you remove the onBackPressed method, I think it will trigger the default back button functionality – red_alert Jul 09 '13 at 09:51
  • no it doesnt . check my first comment. That is what happened when i removed onBackPressed from act1 – Shivam Bhalla Jul 09 '13 at 09:53