finish current activity and start another activity when screen being off (black) - java

i have developed application that consists of A activity (luncher), B activity (second), C activity (third),
what i have tried is :
when the screen is locked or turned off as you are away from the device or have pressed the power button to locked it, my goal is C activity finished and when you turn the screen on again A activity start.
Note : i'm new to android and java development .
i think we can use onPause(), method or onStop(), method for that purpose, but it acually does not work with me as below code in C activity :
protected void onPause(){
super.onPause();
Intent i = new Intent(this,A activity);
startActivity(i);
}
when i lock screen and it become black then open again it still has C activity there.
any advice to get that with onPause(), method or onStop(), method or other ways to get it, thanks

in C Activity - onPause method you can try using this code:
new Handler().post(new Runnable() {
#Override
public void run()
{
Intent intent = new Intent(this,A activity);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
overridePendingTransition(0, 0);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
this will close current activity and open selected activity.
Hope it helps.

I am not sure but try this way:-
protected void onPause()
{
Intent i = new Intent(this,A activity);
startActivity(i);
super.onPause();
}

Related

Is there a method to call activity back when onStop() immediately?

I want to create an app that can't be stopped (send to background). Is there a method to achieve this? this is my code
#Override
protected void onUserLeaveHint()
{
Intent i = new Intent(this, DemoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(i);
}
but this code didnt respond immediately, I search everywehere for solution but get nothing. Thank you.

How to keep the media player keep playing the same sound track and not starting another one in Android

I have an action bar icon on the main activity where when you click on it plays a soundtrack and I can pause and play it properly. I call this code below when the action bar play icon is pressed:
private void play() {
if (!mp.isPlaying()) {
mp.start();//play sound
play=true;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
mp.seekTo(0);
}
});
} else {
mp.pause();
play=false;
}
}
onOptionsItemSelected code:
if (id == com.app.myapp.R.id.play) {
play();
invalidateOptionsMenu();
}
I have another activity that has a home button action icon where when I click it, it takes me back to the main activity and the sound track keeps playing but when I click the play icon again, it plays another instance of the same track which I don't want it to do. I'm use the following code to go back to the main activity:
if (id == com.app.myapp.R.id.homebutton) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
}
What extra piece of code do I need to do what I want my app to do?
Any help would be greatly appreciated.
Jaffer
Every time you launch the main activity from sub activities your main activity is getting created. Since you are creating mediaPlayer instance in onCreate() function new media player instance is created causing two playbacks.
Modify how you launch the main activity like below.
if (id == com.app.twelveimams.R.id.homebutton) {
Log.e("kiran", "clear top from introu");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity

onbackpressed() from Activity to Specific Fragment

I have a MainActivity class with 3 Fragments (each in their own class)
My third fragment (LoginFragment) will allow Login a user and then go to a new activity (new Intent) with some info for that user like the product.
If I press back on that Intent will go back to the LoginFragment.
I override the #OnBackPressed to start the MainActivity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
I need to know how to replace that Fragment with the LauncherFragment (Fragment 1) in MainActivity.
I have this solution but it takes 0.5 sec to 1-2 sec based on device
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
this.finish();
would be cool to go direct to Fragment 1 like to finish the third fragment thanks :)
I fixed the issue in this idea
onBackPressed() I call a new Intent but with extras
In the MainActivity that has the 3 fragments onRestart() I check if it coming from this class ( has that extras ) than go to this fragment (click,replace,delete)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.Intents.NAVIGATE_BACK, true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
}
on the MainActivity I got this
#Override
protected void onRestart() {
super.onRestart();
Intent intent = getIntent();
boolean navigate = intent.getBooleanExtra(Constants.Intents.NAVIGATE_BACK, false);
if (navigate) {
View homeView = bottomNavigationView.findViewById(R.id.home);
homeView.performClick();
intent.removeExtra(Constants.Intents.NAVIGATE_BACK);
}
}
If you want it to be as fast as possible, use one activity and fragments to vary the contents. Adding a fragment doesn't create a new window, whereas an activity does.
Also look at your application logic in fragment / activity startup (onCreate(), onResume(), etc). That's going to be the main factor.

Close another activity from current one

I want to accomplish this structure:
Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.
How can I close activity A and start activity C when activity B is opened?
Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.
I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.
EDIT
I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.
When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows
public void onClick(View v) {
if(v.getId()==R.id.butoonC){
Intent intent = new Intent(this, ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A.
I hope this work for u
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
I use this and worked for me
in Activity A when a button press to go to Activity B use this code :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
ActivityA.this.finish();
}
});
It is close Activity A and when back button pressed do not come back to this Activity.

How To Return To The Main Activity After Destroying A SurfaceView?

My Activity
protected void onDestroy(){
super.onDestroy();
finish();
}
public void onPause(){
super.onPause();
gv.gameLoopThread.setRunning(false);
finish();
}
public void redirectHome(){
onDestroy();
Intent intent=new Intent(PlayActivity.this, MainActivity.class);
startActivity(intent);
}
My View's On Click.
if(gameover){
//My Restart Button.
if(x>(getWidth()*.39375) && x<(getWidth()*.625) &&
y>(getHeight()*.583333333) && y<(getHeight()*.654166667)){
gameover=false;
createSprites();
destroyed=0;
}
//My Exit Button.
if(x>(getWidth()*.39375) && x<(getWidth()*.625) &&
y>(getHeight()*.729166667) && y<(getHeight()*.791666667)){
gameLoopThread.setRunning(false);
new PlayActivity().redirectHome();
}
}
My restart button works, but my exit button causes my app to crash pointing the error at 'Intent intent=new Intent(PlayActivity.this, MainActivity.class);' and 'new PlayActivity().redirectHome();
Any help is appreciated.
You do nt must ot call "OnDestroy()" callback directly and also to call finish() in onDestroy() method.
In your case it would be better to change your code like to something like this:
protected void onDestroy(){
super.onDestroy();
}
public void onPause(){
super.onPause();
gv.gameLoopThread.setRunning(false);
}
public void redirectHome(){
Intent intent=new Intent(PlayActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
Also one question: is this thing is really worked somehow? I'm asking this because finish() call in onPause() callback should close your activity even before it appears on the screen.
Anyway, check my code and comment about results)
EDIT: also you can't actually create instance of activity and call on of it's methods like new PlayActivity().redirectHome(); so you need a context of Аctivity to perform start of new activity or finish this one.
It seems that because you can onDestroy the activity reference PlayActivity.this becomes invalid or null
You can use the application context instead of PlayActivity.this to and start your activity with FLAG_ACTIVITY_NEW_TASK

Categories

Resources