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

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.

Related

Return back to MainActivity after pressing back button from MainActivity

Hi i am newbiew to android i am using back button code in my first activity that is from MainActivity and the app goes to background or exits from the app but when i return back to the app instead of entering the first activity its going to some other page(activity) of the app. I have used the following code
#Override
public void onBackPressed()
{
//super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
//moveTaskToBack(true);
finish();
System.exit(0);
}
I have tried using moveTaskToBack(true) code but it does not refresh the activity, it gives back the first page as such but the login preference does not change. Please someone help me in this task
Just add this lines of code into your launcher activity's oncreate() method
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}

getIntent.remove extra not working

public void showNotification(Context context,String pnrNumber){
Intent intent=new Intent(context,HomeActivity.class);
intent.putExtra("PNR", pnrNumber);
//To Clear the Activity Stack
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNumber,intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TravelKhana")
.setContentText("Get food in train for the PNR:" +pnrNumber);
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(uniqueNumber, mBuilder.build());
uniqueNumber++;
}
and in oncreate of the HomeActivity i am getting this string extra
if(getIntent().hasExtra("PNR")){
mPnrSearch.setTag(getIntent().getStringExtra("PNR"));
onClick(mPnrSearch);
}
And then in onClick(mPnrSearch);
public void onClick(View v) {
switch (v.getId()) {
case R.id.pnrSearch:
if(NetworkChecker.isConnected(getApplicationContext())) {
easyTracker.send(MapBuilder.createEvent("Home Activity","click", "PNR", null).build());
}
Intent pnrIntent = new Intent(HomeActivity.this, PnrSearch.class);
//If the user came from notification
if(v.getTag() != null){
pnrIntent.putExtra("PNR", v.getTag().toString());
v.setTag(null);
getIntent().removeExtra("PNR");
}
startActivity(pnrIntent);
break;
}
I removed the extra and then I pressed the back button to destroy the app and reopened it by long pressing the home button in my phone and then after the extra is still there and onClick(mPnrSearch) is called again, but i have removed the extra why is it so?? and what do i need to do to resolve this out.
This is either an Android bug or a feature, depending on whether you want it to happen or not ;-) This case isn't clearly documented, but it is clear that it behaves the way you describe.
I recently answered a similar question and made some suggestions about how to deal with this problem. See my answer for more information.
i think after removing you should update new intent, just implement this function in your activity
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
setIntent(intent);
}
in my case i passed the value from notification to my MainActivity from onMessageReceived. why because i wanted to count the opening app by notification click for specific notification id which is the value i passed by exteras( notification opens the app mainactivity ) for analytics reasons. what ever, to find out if main activity is opened by notification or not i checked both exteras value and intent flag. i observed that when ever app is opened by notification click the intent flag is 0 so i added the below to my mainactivity.
if(getIntent().getFlags() == 0 && getIntent().getExtras() != null){
Log.i("opened by notification only",getIntent().getStringExtra("myID"));
//rest of process...............
}

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

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

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();
}

How to close an activity (finish()) from service?

i make a lock screen application.. i have some service that listen to the SMS.. when the SMS receiving command andro-lock then i display a lock screen that called by service:
here'e the service's code :
if (tempMessage[0].toString().equalsIgnoreCase("andro-lock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-lock", Toast.LENGTH_LONG).show();
openDatabase();
updateStatusL();
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
}
that code has works perfecly, but i have some problem when i try to unlock my phone.. if my service receive command andro-unlock then i must close (finish) that lockscreen.java.. i was trying many code and still not works.. what must i do to close the lockscreen activity when my service receiving command andro-unlock?? please help..
else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//what must i do??
//any solution??
}
Thanks for your help..
A possible solution to stop the activity would be:
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);
In LockScreenForm
onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
finish();
}
You could send an intent to your activity with an extra, and the activity could read this extra and, if present, finish() itself.
Regards,
Stéphane

Categories

Resources