Why doesn't my application open new Activity from fragment?

时间:2015-10-30 23:28:00

标签: java android android-fragments android-activity

Can someone please tell me what I am doing wrong. I have looked everywhere and tried changing my code many ways and I still can't figure it out. So I have a "LoginActivity.java" file that has a activity_login.xml file. Inside this activity, I have 2 fragments, login_fragment and register_fragment. They each have their own java files. Everything works fine but on my LoginFragment.java file, when the user is verified successfully, it is supposed to open MainActivity, but instead of opening the MainActivity, it is just opening a new instance of my LoginActivity, even though I am specifically telling telling it to open MainActivity. Here is the code in my LoginFragment.java file. package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; public class LoginFragment extends Fragment { private EditText username, password; private RequestQueue requestQueue; private static final String URL = "http://example.com/login.php"; private StringRequest request; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.login_fragment, container, false); username = (EditText) view.findViewById(R.id.lUsername); password = (EditText) view.findViewById(R.id.lPassword); Button signin = (Button) view.findViewById(R.id.login); requestQueue = Volley.newRequestQueue(getActivity()); signin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonObject = new JSONObject(response); if (jsonObject.names().get(0).equals("success")) { Toast.makeText(getActivity(), jsonObject.getString("success"), Toast.LENGTH_SHORT).show(); getActivity().startActivity(new Intent(getActivity(), MainActivity.class)); } else { Toast.makeText(getActivity(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("username", username.getText().toString()); hashMap.put("password", password.getText().toString()); return hashMap; } }; requestQueue.add(request); } }); return view; } }

1 个答案:

答案 0 :(得分:1)

I found the problem and will answer just in case anyone has an issue like mine in the future. So when I start my project, it loads the MainActivity. My MainActivity checks the Shared Preferences on create to check a boolean that states if a user is logged in or not. If no, it opens the LoginActivity, if yes, it continues to launch MainActivity. On the app's first launch, it automatically sets the boolean to false. When I log in, the boolean in the Shared Preferences is supposed to be set to true. Since I forgot to do that, when I try to launch the MainAcitvity, MainActivity looks at the Shared Preferences and sees that userLoggedIn, is set to false, so it launches LoginActivity once again.