2
2
3
3
import android .os .Bundle ;
4
4
5
- import com .google .android .material .snackbar .Snackbar ;
6
-
7
5
import androidx .appcompat .app .AppCompatActivity ;
8
6
7
+ import android .os .Handler ;
8
+ import android .os .Looper ;
9
9
import android .view .View ;
10
-
11
- import androidx .navigation . NavController ;
10
+ import androidx . fragment . app . Fragment ;
11
+ import androidx .fragment . app . FragmentManager ;
12
12
import androidx .navigation .Navigation ;
13
13
import androidx .navigation .ui .AppBarConfiguration ;
14
- import androidx .navigation .ui .NavigationUI ;
15
-
16
14
import com .theriotjoker .beatbot .databinding .ActivityMainBinding ;
17
15
18
- import android .view .Menu ;
19
- import android .view .MenuItem ;
20
- import android .view .Window ;
16
+
21
17
import android .view .WindowManager ;
18
+ import android .widget .Toast ;
22
19
23
20
import java .util .Objects ;
24
21
25
22
public class MainActivity extends AppCompatActivity {
26
23
27
24
private AppBarConfiguration appBarConfiguration ;
28
25
private ActivityMainBinding binding ;
26
+ private boolean pressedBackRecently = false ;
29
27
30
28
@ Override
31
29
protected void onCreate (Bundle savedInstanceState ) {
@@ -39,33 +37,34 @@ protected void onCreate(Bundle savedInstanceState) {
39
37
getWindow ().setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN , WindowManager .LayoutParams .FLAG_FULLSCREEN );
40
38
41
39
}
42
-
43
- @ Override
44
- public boolean onCreateOptionsMenu (Menu menu ) {
45
- // Inflate the menu; this adds items to the action bar if it is present.
46
- getMenuInflater ().inflate (R .menu .menu_main , menu );
47
- return true ;
48
- }
49
-
50
40
@ Override
51
- public boolean onOptionsItemSelected (MenuItem item ) {
52
- // Handle action bar item clicks here. The action bar will
53
- // automatically handle clicks on the Home/Up button, so long
54
- // as you specify a parent activity in AndroidManifest.xml.
55
- int id = item .getItemId ();
56
-
57
- //noinspection SimplifiableIfStatement
58
- if (id == R .id .action_settings ) {
59
- return true ;
41
+ public void onBackPressed () {
42
+ //This is a quick, but dirty way of making the back button transition back to the main screen
43
+ FragmentManager fragmentManager = getSupportFragmentManager ();
44
+ Fragment navHostFragment = fragmentManager .findFragmentById (R .id .nav_host_fragment_content_main );
45
+ Fragment currentFragment = navHostFragment .getChildFragmentManager ().getFragments ().get (0 );
46
+ if (currentFragment instanceof ResultScreen ) {
47
+ Navigation .findNavController (navHostFragment .getView ()).navigate (R .id .fileScreenToMainScreen );
48
+ } else {
49
+ MainScreen mainScreen =(MainScreen )currentFragment ;
50
+ if (mainScreen .isRecording ()) {
51
+ mainScreen .stopRecording ();
52
+ } else {
53
+ if (!pressedBackRecently ) {
54
+ Toast .makeText (currentFragment .requireContext (), "Please press back again to exit the app" , Toast .LENGTH_SHORT ).show ();
55
+ pressedBackRecently = true ;
56
+ new Handler (Looper .getMainLooper ()).postDelayed (new Runnable () {
57
+ @ Override
58
+ public void run () {
59
+ pressedBackRecently = false ;
60
+ }
61
+ }, 1500 );
62
+ } else {
63
+ pressedBackRecently = false ;
64
+ finish ();
65
+ }
66
+ }
60
67
}
61
68
62
- return super .onOptionsItemSelected (item );
63
- }
64
-
65
- @ Override
66
- public boolean onSupportNavigateUp () {
67
- NavController navController = Navigation .findNavController (this , R .id .nav_host_fragment_content_main );
68
- return NavigationUI .navigateUp (navController , appBarConfiguration )
69
- || super .onSupportNavigateUp ();
70
69
}
71
70
}
0 commit comments