Skip to content

Commit

Permalink
Use OnBackPressedDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Apr 22, 2024
1 parent 8e8b6ea commit 21c7389
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;

/**
Expand All @@ -41,6 +42,17 @@ protected void onCreate(Bundle savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, clientSettingsFragment)
.commit();

getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Intent intent = new Intent();
intent.putExtra("client", clientSettingsFragment.getClient().toJson().toString());
intent.putExtra("clientOriginal", clientSettingsFragment.getOriginalClientInfo().toJson().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}

@Override
Expand All @@ -51,14 +63,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return false;
}

@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("client", clientSettingsFragment.getClient().toJson().toString());
intent.putExtra("clientOriginal", clientSettingsFragment.getOriginalClientInfo().toJson().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;

/**
Expand All @@ -42,6 +43,20 @@ protected void onCreate(Bundle savedInstanceState) {
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, groupSettingsFragment).commit();

getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Intent intent = new Intent();
intent.putExtra("group", groupSettingsFragment.getGroup().getId());
if (groupSettingsFragment.didStreamChange())
intent.putExtra("stream", groupSettingsFragment.getStream());
if (groupSettingsFragment.didClientsChange())
intent.putStringArrayListExtra("clients", groupSettingsFragment.getClients());
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}

@Override
Expand All @@ -52,18 +67,5 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return false;
}

@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("group", groupSettingsFragment.getGroup().getId());
if (groupSettingsFragment.didStreamChange())
intent.putExtra("stream", groupSettingsFragment.getStream());
if (groupSettingsFragment.didClientsChange())
intent.putStringArrayListExtra("clients", groupSettingsFragment.getClients());
setResult(Activity.RESULT_OK, intent);
finish();
}
}

0 comments on commit 21c7389

Please sign in to comment.