Difficulty: Medium
Correct Answer: Intent intent = new Intent(CurrentActivity.this, NewActivity.class); startActivity(intent);
Explanation:
Introduction / Context:
Android applications are built from components such as activities, services, and broadcast receivers. To move from one screen to another, you typically start a new activity using an Intent. Understanding the correct pattern for constructing an explicit intent and calling startActivity from within an activity is essential in Android programming interviews and exams.
Given Data / Assumptions:
Concept / Approach:
To start a new activity from the current activity, Android developers typically create an explicit Intent that directly names the target activity class. The Intent constructor takes a Context and the target class reference. Then they pass this Intent to startActivity. The context parameter is often written as CurrentActivity.this or getApplicationContext(). This mechanism asks the Android system to create and start the new activity, placing it on top of the activity stack.
Step-by-Step Solution:
Step 1: Recognize that the correct answer must involve the Intent class and the startActivity method, both part of the Android framework.Step 2: Examine option A. It constructs a new Intent with CurrentActivity.this as the context and NewActivity.class as the target, then calls startActivity(intent). This matches the standard pattern for launching an activity.Step 3: Examine option B. It manually creates a NewActivity instance on a new thread. This is not how activities are started in Android, and it does not use the framework lifecycle correctly.Step 4: Examine option C. It only prints a message to the console and does not interact with the Android runtime at all.Step 5: Examine option D. It creates an empty Intent without setting a target activity, so calling startActivity with such an intent would either fail or require additional configuration.
Verification / Alternative check:
Android documentation and tutorials consistently show the explicit intent pattern: Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); startActivity(intent);. This snippet is widely used for navigation between activities. Sample projects generated by Android Studio also use similar code when wiring up buttons that open new screens.
Why Other Options Are Wrong:
Option B bypasses the Android component model and lifecycle entirely, which is not supported and would not display a new activity screen.Option C simply logs text to standard output and does not involve activities or intents.Option D constructs an Intent without specifying any destination, which is incomplete for launching a new activity.
Common Pitfalls:
New Android developers sometimes try to instantiate activities with the new operator or start them from background threads, which is incorrect. Activities must be launched through the framework using intents from the main thread. Another pitfall is using the wrong context, such as an application context when an activity context is required. Using CurrentActivity.this in the constructor is a safe and common pattern from within an activity class.
Final Answer:
The correct answer is Intent intent = new Intent(CurrentActivity.this, NewActivity.class); startActivity(intent);.
Discussion & Comments