Question

Est-il possible de modifier dynamiquement l'activité à partir de Android basée sur une condition? Ce que je tentais de faire (qui n'a pas de travail) était la suivante:

  1. supprimer la catégorie LANCEUR tel que défini dans mon AndroidManifest.xml
  2. créer une classe d'application personnalisée que les utilisations app
  3. remplacer la méthode onCreate de ma classe d'application pour définir un code comme suit:

.

if (condition) {
    startActivity(new Intent(this, MenuActivity.class));
} else {
    startActivity(new Intent(this, LoginActivity.class));
}
Était-ce utile?

La solution

Why not have an initial Activity with no UI that checks the condition in its onCreate, then launches the next Activity, then calls finish() on itself? I've never called finish() from within onCreate() though, so I'm not sure if this will work.

EDIT
Seems to work fine. Here's some code to make it clearer.
Initial Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent;
    if (condition) {
       intent = new Intent(this, ClassA.class);
    } else {
       intent = new Intent(this, ClassB.class);
    }
    startActivity(intent);
    finish();
    // note we never called setContentView()
}

Other Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Autres conseils

Here's what I personally did for one of my small mobile projects. Instead of creating a separate, screen-less Activity where the condition is and which launches the corresponding screen, I put the condition in one Activity and did a dynamic setContentView(), as in:

if (!userIsLoggedIn) {
    setContentView(R.layout.signup);
} else {
    setContentView(R.layout.homescreen);
}

Two important notes to this approach:

1: Instead of writing that in onCreate(), you want to put the decision-making inside onResume() precisely because the latter is always called whenever the screen needs to be displayed in front. You can see that from the Android activity life cycle. So if, for example, the user just downloaded my app and launched it for the first time, because no user is logged in, she will be led to the signup page. When she's done signing up and for some reason presses the HOME button (not BACK, which exits the app altogether!) and then resumes the app, the layout that she will see is already the home screen's. If I put the conditional inside onCreate(), what would have been displayed is the sign up screen because according to the life cycle, it doesn't go back to onCreate() when bringing back an app to the front.

2: This solution is ideal only if merging the functionalities of those two Activities would not produce a long diabolical block of code. Like I said, my project was a small one (its primary feature occurs in the background), so that single dynamic Activity didn't have too much in it. The screen-less Activity is definitely the way to go if you need your code to be more human-readable.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top