简体   繁体   中英

startActivity() doesn't start activity

I have a class called ShowBoardList where I check if user has logged in. If user hasn't logged in, then I want to return to the MainActivity which provides the user with buttons to login into different services.

My AndroidManifests.xml looks like this:

<application
   <activity android:name="im.chaitanya.TaskTimer.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="im.chaitanya.TaskTimer.WebViewActivity" >
    </activity>
    <activity android:name="im.chaitanya.TaskTimer.ShowBoardList"
        android:label="Your Tasks">
    </activity>
</application>

ShowBoardList.java looks like this:

...
Intent mainActivityIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
Intent intent = getIntent();
String url = intent.getStringExtra(WebViewActivity.EXTRA_MESSAGE); //url can be null here
Keys keys = new Keys(); //this gets an API key

SharedPreferences settings = getSharedPreferences("mySettings", 0);
String savedToken = settings.getString("token", "Empty");

if (MyUtils.equalsWithNulls(url,"tasktimer://oauthresponse#token=")) {
    Log.d("From ME:", "I've reached inside url check");
    mainActivityIntent.putExtra(caller, "ShowBoardList");
    //caller is a String. I'm storing the name of the current activity (ShowBoardList) in it. 
    //So that the main activity (which I'm trying to call) will know where the call came from.
    startActivity(mainActivityIntent);
}

if(savedToken.equals("Empty") || savedToken.equals("")) {
    String searchString = "#token=";
    int tokenIndex = url.indexOf(searchString) + searchString.length(); //Since url can be null there can be an error here
    String token = url.substring(tokenIndex);
    savedToken = token;
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("token", token);
    editor.apply();
}
...

Condtion equalsWithNulls checks if url is null OR equal to the string in the argument. I have log statements there to check whether control reaches inside the if statement. The main activity however doesn't start.

Edit: onCreate() of MainActivity.java looks like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences settings = getSharedPreferences("mySettings", 0);
    String token = settings.getString("token", "Empty");
    Intent intent = new Intent(this, ShowBoardList.class);

    if(token != "Empty") {
        startActivity(intent);
    }

    intent = getIntent();
    String callerActivity = intent.getStringExtra(ShowBoardList.caller);
    View coordinatorLayoutView = findViewById(R.id.snackbarPosition);

    if (callerActivity!=null && callerActivity == "ShowBoardList") {
        Snackbar
                .make(coordinatorLayoutView, "Permission Denied", Snackbar.LENGTH_LONG)
                .show();
    }
    setContentView(R.layout.activity_main);
}

Try to define your new Intent wherever you required.

Intent newIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
newIntent .putExtra(caller, "ShowBoardList");

startActivity(newIntent );

My solution is based on Sourabh's comment on the question. I realised from my logs that the activity was indeed being started.

What I didn't realise was that when startActivity() is called, the calling activity (in this case ShowBoardList ) is paused and when ShowBoardList was being called again, it would resume from after startActivity() .

Therefore the solution here was to call finish() and then return immediately after the startActivity() which ensures that onCreate is called the next time. I hope that makes sense if anyone is in the same situation.

These questions helped me understand more about finish() :

about finish() in android

onCreate flow continues after finish()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM