0

I set the <button> in activity_main.xml

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="ReturnHome"
    android:text="Go Back"
    android:layout_marginBottom="79dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

And now I'd like to add onBackPressed method in AnswerActivity

public class AnswerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_answer);
    TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);

To be able to go back to previous page which is Fragment.

Can somebody guide me step by step how to do it?

Edit:

I wrote something like this

     super.onBackPressed();
        Intent returnIntent = new Intent();
        finish();

But it says 'variable "returnIntent" is never used'. How to fix it?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ramona
  • 159
  • 1
  • 10

1 Answers1

0

If you meant the physical button on the device, this is done by overriding:

@Override
public void onBackPressed() {
    FragmentManager fm = getActivity().getSupportFragmentManager();
    fm.popBackStack();
}

And your code:

super.onBackPressed();
    Intent returnIntent = new Intent();
    finish();

Shows returnIntent is never shown because you never called startActivity (intent). And you have to make sure you add a target.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • did you read the question properly? OP want to go to the previous fragment. – N J May 14 '17 at 15:35
  • Clearly the question was clear. Though he want to go to previous fragment on onBackPressed but he still shows us xml code for Button – Swarnveer May 14 '17 at 15:36
  • @Swarnveer, so what 'she' :) can show you to be more specific and clear? – Ramona May 14 '17 at 15:41
  • Sorry for "he" I didn't knew. And earlier I meant, "the question wasn't clear". It was was a type – Swarnveer May 14 '17 at 15:46
  • @Swarnveer, no problem at all :) Just please help me if you can. – Ramona May 14 '17 at 15:57
  • @LunarWatcher. I appreciate your efforts. It is as you said a `physical button` and your second option is a good way to follow. But what to write inside this method `onBackPressed`? – Ramona May 14 '17 at 16:05
  • You need to override onBackPressed using Ctrl + O – Swarnveer May 14 '17 at 17:28
  • I didn't understand what is the Button for when you want things to be done in onBackPressed – Swarnveer May 14 '17 at 17:28
  • @Swarnveer the xml with the button threw me off, first OP says (s)he wants to use onBackPressed while showing XML code for a .xml button. I showed both solutions as it is very hard to understand which OP wanted to achieve. – Zoe May 14 '17 at 19:55
  • @Ramona You use this code: `FragmentManager fm = getActivity().getSupportFragmentManager(); fm.popBackStack();` – Zoe May 14 '17 at 19:56
  • @LunarWatcher. Many thanks Lunar. Tell me please how to 'add this target'? I get now `getActivity` in red. – Ramona May 14 '17 at 20:19
  • That is from a fragment. If you call it from an activity remove getActivity – Zoe May 15 '17 at 13:29
  • Many thanks Lunar. Could you please have a look at my other issue?http://stackoverflow.com/questions/43945129/getting-constant-false-value-from-method – Ramona May 15 '17 at 14:29
  • @LunarWatcher. Hi Lunar. Just came accross your answer and thought maybe you would be able to help with this issue..https://stackoverflow.com/questions/43997655/zoom-in-out-the-whole-page-layout. Many thanks in advance. – Ramona May 16 '17 at 09:51
  • @Ramona looking at both those questions. But if this answer worked for you, please mark it as the solution(to let others know this problem is answered and solved) – Zoe May 16 '17 at 15:47