1

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);
}
Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
  • 1
    Is there any error? What happened? LogCat please. – Uma Kanth Oct 24 '15 at 07:17
  • I used the debugger to check what is happening. The control goes over that statement and on to the next line outside the if statement. There's no error over there. There *is* an error later on but that is because the the main activity isn't started and the program tries to call functions on a null object. – Chaitanya Nettem Oct 24 '15 at 07:21
  • I added some more code. That should make it more clear as to what I'm trying to accomplish. But like I said there are no errors caused at the startActivity() statement. – Chaitanya Nettem Oct 24 '15 at 07:27
  • `startActivity()` is not synchronous. It returns immediately and the activity gets started later. Is this your problem? – laalto Oct 24 '15 at 07:29
  • It might be. How do I wait for the activity to start? – Chaitanya Nettem Oct 24 '15 at 07:30
  • Does your logcat prints this line "From ME:", "I've reached inside url check " ? – KishuDroid Oct 24 '15 at 07:30
  • @KishuDroid - yes it does – Chaitanya Nettem Oct 24 '15 at 07:31
  • Okey so what it caller ? – KishuDroid Oct 24 '15 at 07:32
  • @KishuDroid - 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. – Chaitanya Nettem Oct 24 '15 at 07:34
  • can you paste the mainActivity here... may be it is started and come back to ShowBoardList again... due to conditions check on mainActivity. – SRB Bans Oct 24 '15 at 07:39
  • @sourabhbans - I added that. ShowBoardList is indeed started from the mainactivity. However that only happens when the token variable has already been saved in ShowBoardList. This saving happens after the startActivity call is made in ShowBoardList. I'll try adding a couple of Log statements to see if it comes back and then again goes to ShowBoardList. – Chaitanya Nettem Oct 24 '15 at 07:46
  • @sourabhbans - You're right. ShowBoardList is being called again from MainActivity. Thanks for that! I need to see why this is happening now. – Chaitanya Nettem Oct 24 '15 at 07:51
  • @sourabhbans - No. Scratch that. I miss read the log. That is not the problem. – Chaitanya Nettem Oct 24 '15 at 07:53
  • try to use `(token!=null && !(token.equals( "Empty")))`... in stead of `(token != "Empty")` .. it(!=) will never check for strings. – SRB Bans Oct 24 '15 at 08:03

2 Answers2

1

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 );
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0

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()

Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45