Android: How to restart application with first activity

You can restart first activity in android using following method:

</code>

private void restartFirstActivity()
 {
 Intent i = getBaseContext().getPackageManager()
 .getLaunchIntentForPackage(getBaseContext().getPackageName() );

 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
 startActivity(i);
 }

<code>

About these ads

About M Moniruzzaman
A passionate software engineer, have been developing applications on various platforms such as Android, iPhone, .Net (C#) technologies and web based ASP.NET, PHP, JavaScript, jQuery technologies for more than 5 years. Especially I have expertise on developing applications for Android and iPhone, as well as service oriented, client-server based applications where clients will be reside on Android/iPhone that communicate with WCF(.NET) service hosted on server. I have completed certification in Microsoft Certified Professional Developer (MCPD) on .Net 4 . I have completed my graduation in -- B.Sc in Computer Science and Engineering, ShahJalal University of Science and Technology, Bangladesh. Thanks, M. Moniruzzaman (Zaman)

7 Responses to Android: How to restart application with first activity

  1. sprin says:

    I have the following problem: I’m doing some tests on applications with robotium and i want to restart the application from within the test case. Below is the code I’m working on

    public testtest(){
    super(“it.esercizio141.esempiomenu”,EsempioMenuActivity.class);

    }

    public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testFai() throws Exception {
    solo.pressMenuItem(0);
    solo.pressMenuItem(1);
    this.launchActivity();

    }
    Where can I go to write something in order to restart the application? Thank you very much

  2. Zaman says:

    Hi,
    It is for restarting first activity of the application from any other activity.

    For example, the android application name is “MyApp” and with an activity for login “LoginActivity”. When the application starts it lunches LoginActivity, after successful loggedin it may open new other activities such as DisplayInfoActivity1, DisplayInfoActivity2 and so on. Then if it is needed that you want to restart from first LoginActivity wherever it is now, you can call this method which reside in that activity from where it’s necessary to restart, for example, in DisplayInfoActivity2.

  3. Hartmut Vogelsang says:

    Hey, thanks! Works great and helped me a lot! Very clear approach.

    Best Hartmut

  4. Nikhil Gopal says:

    Thanks. Simple and clean, just the way I like it.

  5. Rameez RajpuT says:

    Its Not Working,,.. i Installed Sygicv1102cnst.apk after tht, its appear download the map..i cancelled & Restart my Mobile..Even All Map Still in my SDcard..but When i open Apps Sygic..its force for download data…
    <>

    .

  6. Shahwar Salee says:

    did the job for me

  7. Sumon Chatterjee says:

    Hi
    The application is actually not getting restarted.It is getting launched from the first activity but with the same PID and even after launching when device back button is clicked it goes back previous activity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: