How to close an application?

Put your problem here if it does not fit any of the other categories.

How to close an application?

Postby NithinG » Fri Mar 28, 2008 10:16 am

hi all,

I need to close my application. For this i trie dwith the function finish(). But it is closing the current displayed screen only. Then i tried with System.exit(). Through this method the application is exiting but it'll restart automatically.
Can anyone help me to solve this problem.... :cry:

Thanks and Regards,

NithinG
NithinG
Developer
Developer
 
Posts: 36
Joined: Wed Feb 27, 2008 6:24 am

Top

Postby Jesmiatka » Fri Mar 28, 2008 10:36 am

Hi,

I do not know if this will work but why don't you try the following:

Try getting all the activities in the application and call a finish on them.
Maybe the activitymanager can help (I see a getRunningTasks which returns a list)

http://code.google.com/android/referenc ... nager.html
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby NithinG » Sat Mar 29, 2008 5:20 am

hi Jesmiatka,
Can you tell me how to do it.. I tried something like this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ActivityManager manager = null ;
  2.  
  3. List<RunningTaskInfo> list = manager.getRunningTasks(3);
  4.  
  5. for( int i = 0 ; i < list.size() ; i++ )
  6.  
  7. {
  8.  
  9.         list.get(i);
  10.  
  11.         finish();
  12.  
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


But this is not correct.. i dont know how to prceed further. Can you give me a suggestion.

Thanks,
NithinG
NithinG
Developer
Developer
 
Posts: 36
Joined: Wed Feb 27, 2008 6:24 am

Postby plusminus » Sat Mar 29, 2008 4:30 pm

Hello NithinG,

when I'm closing a series of SubActivities, I do the following custom code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         this.setResult(Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
  2.  
  3.         this.finish();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and in onActivityResult() of the previous Activity I write:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         protected void onActivityResult(int requestCode, int resultCode,
  4.  
  5.                         String data, Bundle extras) {
  6.  
  7.                 switch(resultCode){
  8.  
  9.                         case Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE:
  10.  
  11.                                 this.setResult(Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
  12.  
  13.                                 this.finish();
  14.  
  15.                                 break;
  16.  
  17.                 }
  18.  
  19.                 /* Finally call the super()-method. */
  20.  
  21.                 super.onActivityResult(requestCode, resultCode, data, extras);
  22.  
  23.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


works absolutely fluent (and Activities do not pop up in between.).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby NithinG » Mon Mar 31, 2008 11:10 am

hi Plusminus,

Thanks im getting it now

NithinG
NithinG
Developer
Developer
 
Posts: 36
Joined: Wed Feb 27, 2008 6:24 am

Postby pratik goswami » Thu Oct 16, 2008 8:07 am

Hey plusminus

There is no Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE in Sdk 1.0........What is Constants actually in given Examle? :?: Is there any alternate way to achieve the same functionality?

regards
Pratik
pratik goswami
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Sep 22, 2008 2:54 pm

Top

Postby MrSnowflake » Thu Oct 16, 2008 9:05 am

It's just a contant he defined himself in the (static) class Constants. Just use your own id.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Fri Jan 02, 2009 7:55 am

Slightly different,

In the second activity>>
-----
Bundle bundle = new Bundle();
bundle.putInt("Close", 1);
Intent i = new Intent();
i.putExtras(bundle);
setResult(RESULT_OK, i);
finish();
------

In OnActivityResult() of the first class>>
-----
Bundle extras = intent.getExtras();
if(extras.getInt("Close") == 1);
finish();
break;
-----
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby MrSnowflake » Fri Jan 02, 2009 2:16 pm

I don't get what advantage your method has over PlusMinus'.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby immanueln2005 » Fri Jan 02, 2009 8:20 pm

Hardly anythin...

I'm just using the intent to return a constant rather than a constant in another class...

I'm still juvenile at Android... Pardon me if tat really doesn't make much diff :D
immanueln2005
Developer
Developer
 
Posts: 42
Joined: Tue Dec 30, 2008 1:25 pm
Location: INDIA

Postby MrSnowflake » Sat Jan 03, 2009 2:19 pm

immanueln2005 wrote:Hardly anythin...

I'm just using the intent to return a constant rather than a constant in another class...

I'm still juvenile at Android... Pardon me if tat really doesn't make much diff :D
There is not _wrong_ with your approach, but PlusMinus' approach is just more the way to go :). Because he uses the feature of subActivities (startActivityForResult()).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby kali » Thu Feb 26, 2009 7:19 am

hai friend
can we restart an emulator from code.Is there any method for it
such to exit from application we are using finish method().
kali
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 27, 2009 1:31 pm

Postby practicallysimple » Tue Nov 03, 2009 11:26 pm

plusminus wrote:Hello NithinG,

when I'm closing a series of SubActivities, I do the following custom code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         this.setResult(Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
  2.         this.finish();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


and in onActivityResult() of the previous Activity I write:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         protected void onActivityResult(int requestCode, int resultCode,
  3.                         String data, Bundle extras) {
  4.                 switch(resultCode){
  5.                         case Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE:
  6.                                 this.setResult(Constants.SUBACTIVITY_RESULTCODE_CHAINCLOSE);
  7.                                 this.finish();
  8.                                 break;
  9.                 }
  10.                 /* Finally call the super()-method. */
  11.                 super.onActivityResult(requestCode, resultCode, data, extras);
  12.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


works absolutely fluent (and Activities do not pop up in between.).

Regards,
plusminus


I have a similar solution in my code for using the 'Quit' option available in the menu options for my application.

However, I found a funny situation that happens when the user presses the 'Home' button while my application is running.

I found that even with using the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. finish();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
command, when the Home button is used, an instance of my application is saved in memory.

When my application is opened again, it starts from the beginning. However, if another "saved" instance exists in memory, when I click on the "Quit" option in my menu selections, which executes
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. finish();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
for each level of activities called in my application, the previously "saved" instances of my application are resumed after the newly begun instance of the application exits.

If you find my above explanation confusing, then you can relate to how confusing it is becoming troubleshooting how to effectively "close" an application.

So far, it seems the only way to close an application is to make sure each activity is closed in the application, and to do that, every event that can interrupt and pause the application must be individually handled, so that
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. finish();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
is called, and memory objects are cleaned for each open activity in the application.
practicallysimple
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Sep 16, 2009 8:37 pm
Location: Boise

Anyone help on how to implement Back button functionality.

Postby srinuv28 » Wed Nov 04, 2009 7:25 am

Hi.

When I press back button it goes out of my application(i.e Android Home screen).
anyone help.

Thanks&Regards
Srinivas
srinuv28
Developer
Developer
 
Posts: 36
Joined: Wed Nov 04, 2009 4:04 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 14 guests