0

I am having a problem using the intent. As you can see below I have an intent under Log in, in which, if it is true, it will bring me to "HomePageActivity". The problem is, It doesn't let me go to "HomePageActivity". I'm pretty sure "HomePageActivity" is correct. This problem happens to all buttons at all activities. Nothing lets me to go back to my "HomePageActivity". This problem is easily fixed when I change "HomePageActivity" to some other activities and it will go to that activity. But I want to go to "HomePageActivity". Can anyone help me go home please?

This is the code to my "SignInActivity"

public class SignInActivity extends ActionBarActivity {

protected EditText mUsernameSignIn;
protected EditText mPasswordSignin;
protected Button mSignInButton;
protected Button mSignUpButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    //Initialize
    mUsernameSignIn = (EditText) findViewById(R.id.usernameSignIn);
    mPasswordSignin = (EditText) findViewById(R.id.passwordSignIn);
    mSignInButton = (Button) findViewById(R.id.signinBtnSignIn);
    mSignUpButton = (Button) findViewById(R.id.signupBtnSignIn);

    //Listen to when the log in mSignIn Button is clicked
    //Sign in the user using Parse SDK
    mSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //get the user inputs from edit text and covert to string
            String username = mUsernameSignIn.getText().toString().trim();
            String password = mPasswordSignin.getText().toString().trim();
            //Loging In

            ParseUser.logInInBackground(username, password, new LogInCallback() {
                @Override
                public void done(ParseUser parseUser, com.parse.ParseException e) {
                    if (e == null) {
                        //Success!
                        Toast.makeText(SignInActivity.this, "Welcome Back", Toast.LENGTH_LONG).show();

                        //bring user to HomePageActivity
                        Intent GoHome = new Intent(SignInActivity.this, HomePageActivity.class);
                        startActivity(GoHome);


                    } else {
                        //Sorry, Sign in Failed!

                        AlertDialog.Builder builder = new AlertDialog.Builder(SignInActivity.this);
                        builder.setMessage(e.getMessage());
                        builder.setTitle("Sorry, Sign In Failed");
                        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int which) {
                                //close the dialog
                                dialogInterface.dismiss();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }

                }
            });
        }
    });

    //Go Sign Up
    mSignUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent GoSignUp = new Intent(SignInActivity.this, RegisterActivity.class);
            startActivity(GoSignUp);
        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_sign_in, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

And this is the code to my "HomePageActivity"

public class HomePageActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    // Enable Local Datastore.
    Parse.enableLocalDatastore(this);

    Parse.initialize(this, "EshgpTdW5ialGcLR7ing2x4jhkkgmbHBVeKwLh0J", "av66d87CSHlbRElTPPRx5jLWGefLYwfZ5Y4GHsZ6");
    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();
}

//add pet button brings to add pet activity
public void addPetBtn(View v) {
    Intent intent = new Intent(HomePageActivity.this, AddPetActivity.class);
    startActivity(intent);
}

//register button brings to registration activity
public void registerBtn(View v) {
    Intent intent = new Intent(HomePageActivity.this, RegisterActivity.class);
    startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case R.id.action_update:
            //take user to update activity
            break;

        case R.id.action_log_out:
            // take user to log out activity

            ParseUser.logOut();
            //take user back to Log in Screen
            Intent GoHome = new Intent(HomePageActivity.this,SignInActivity.class);
            startActivity(GoHome);
            break;


    }

    return super.onOptionsItemSelected(item);
}

}

And this is the Android Manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HomePageActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AddPetActivity"
        android:label="@string/title_activity_add_pet" >
    </activity>
    <activity
        android:name=".RegisterActivity"
        android:label="@string/title_activity_register" >
    </activity>
    <activity
        android:name=".SignInActivity"
        android:label="@string/title_activity_sign_in" >
    </activity>
</application>

stanley santoso
  • 343
  • 1
  • 6
  • 19

2 Answers2

0

If you want to go back to main activity, you can call finish() to close the current activity. seems this may help you

Community
  • 1
  • 1
Ken Kwok
  • 388
  • 3
  • 19
0

Instead of starting a HomePageActivity by

Intent GoHome = new Intent(SignInActivity.this, HomePageActivity.class);
startActivity(GoHome);

You can finish the current activity by:

finish();

This is because your HomePageActivity is starting the other two activities, and you can use the finish() method in those activities to go back to HomePageActivity.

Read more here : Android: Go back to previous activity

Community
  • 1
  • 1
Manan
  • 929
  • 9
  • 11
  • Okay, But why is it not working? I can do make my activities go from signup>signin>signup> and back to sign in without any problem. But I cannot go to my HomPageActivity? – stanley santoso Jun 29 '15 at 03:45
  • I am following a tutorial and they are doing it the way that is shown in my code and theirs work. – stanley santoso Jun 29 '15 at 03:45
  • and because of this problem, I cannot start my app from the SignInActivity and go to HomePageActivity after signing in. – stanley santoso Jun 29 '15 at 03:49
  • i cant say anything about that unless you put up your debug log. (logcat output) – Manan Jun 29 '15 at 11:31
  • This is the LogCat: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.......HomePageActivity}: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)` I dont understand. I already initialize and enable local data store in the homepageactivity. what is the problem? – stanley santoso Jul 02 '15 at 04:12
  • @stanleysantoso give us the LogCat error that you are seeing AFTER clicking the button. The one you gave above is related to Parse, not the issue we're talking about. – Manan Jul 02 '15 at 15:37
  • Exactly! And that is the logcat i got after clicking the button. – stanley santoso Jul 02 '15 at 15:41
  • Maybe if we solve the parse problem, this problem will also be solved? – stanley santoso Jul 02 '15 at 15:42
  • Read this : http://stackoverflow.com/questions/30135858/parse-error-parseenablelocaldatastorecontext-must-be-invoked-before-parse – Manan Jul 02 '15 at 15:46
  • I think, because the `Parse.initialize()` is already run when the HomePageActivity is created, and then we try to create HomePageActivity once again, then `Parse.enableLocalDataStore()` would be called once again, and this might be causing the problem... – Manan Jul 02 '15 at 15:49
  • Interesting. I understand that. So the link you gave me makes parse initialize to be a global thing that launches when the app launch? This way it will not be duplicated? – stanley santoso Jul 02 '15 at 15:51
  • yes, exactly, the `Parse.enableLocalDataStore()` wouldn't be called twice if you do that. – Manan Jul 02 '15 at 15:52
  • Another thing you can do is, check whether localDataStore is enabled before enabling it. – Manan Jul 02 '15 at 15:53
  • Okay. I will try that. I will need to remove them from HomePageActivity as well right? – stanley santoso Jul 02 '15 at 15:53
  • Master, can you help me out [here](http://stackoverflow.com/questions/31225652/how-to-show-image-from-gallery-to-imageview) – stanley santoso Jul 04 '15 at 22:41