start activity inside another activity

243
March 26, 2017, at 12:07 PM

I have a class that looks like this:

public class GoogleDriveSync extends AppCompatActivity {
     // What Happens When App Is Resumed//
     @Override
     protected void onStart() {
         // Where I Want To Start Inner Class//
         super.onStart();
     }
     private class SyncReminders extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
         @Override
         public void onConnected(@Nullable Bundle bundle) {
         }
         @Override
         public void onConnectionSuspended(int i) {
         }
         @Override
         public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
     }
}

Inside that Class I have another class. The main class will eventually have three private classes in it. How do I start the inner class using an intent. Thanks!

Answer 1

It would have to be a public static subclass of Activity (or AppCompatActivity, as you have it here), not a private class. You would then start it via startActivity(), the same way that you start up any other activity.

However, it is unclear why you are doing any of this. Starting an activity from onStart() of another activity is unlikely to be want you want.

Let's walk through what will happen, assuming that your strangely-named GoogleDriveSync activity is the launcher activity:

  • User taps your launcher icon
  • Android creates an instance of GoogleDriveSync
  • Android calls onCreate() on your GoogleDriveSync instance
  • Android calls onStart() on your GoogleDriveSync, where you call startActivity() to start SyncReminders
  • Android calls onStop() on your GoogleDriveSync, as it will no longer be visible (assuming that SyncReminders is a typical activity, filling the screen)
  • Android creates an instance of SyncReminders, goes through its lifecycle methods, and shows it
  • Eventually, the user tries pressing BACK
  • Android calls onStart() on your GoogleDriveSync, where you call startActivity() to start SyncReminders
  • Android calls onStop() on your GoogleDriveSync, as it will no longer be visible (assuming that SyncReminders is a typical activity, filling the screen)
  • Android creates an instance of SyncReminders, goes through its lifecycle methods, and shows it
  • Eventually, the user tries pressing BACK
  • Android calls onStart() on your GoogleDriveSync, where you call startActivity() to start SyncReminders

And so on.

In other words, the UI of GoogleDriveSync will never be shown, and the user will wonder why she cannot exit SyncReminders.

Unless there is a clear reason for using two activities here, use just one activity.

READ ALSO
Android - Custom calendar reminder for calling a user

Android - Custom calendar reminder for calling a user

I am currently building an app with the following use case:

250
How do i send a value from my async task to another new activity?

How do i send a value from my async task to another new activity?

I am calling convertResponseToString in my async task however i also have a button, how do i get the logofeature to show the ouput in a new activity? Basically i want to show the message in a new activity

257
Restart service

Restart service

Is it possible to create service wich will restart after the main app is closed? I've tryied return START_STICKY; it onStartCommand() method and I don't want to use startForeground(); because I need service without notification

437
How to get a resource id with a known resource name?

How to get a resource id with a known resource name?

I want to access a resource like a String or a Drawable by its name and not its int id

458