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


can please tell me doing wrong. have looked everywhere , tried changing code many ways , still can't figure out. have "loginactivity.java" file has activity_login.xml file. inside activity, have 2 fragments, login_fragment , register_fragment. each have own java files. works fine on loginfragment.java file, when user verified successfully, supposed open mainactivity, instead of opening mainactivity, opening new instance of loginactivity, though telling telling open mainactivity. here code in 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;     }  } 

i found problem , answer in case has issue mine in future. when start project, loads mainactivity. mainactivity checks shared preferences on create check boolean states if user logged in or not. if no, opens loginactivity, if yes, continues launch mainactivity. on app's first launch, automatically sets boolean false. when log in, boolean in shared preferences supposed set true. since forgot that, when try launch mainacitvity, mainactivity looks @ shared preferences , sees userloggedin, set false, launches loginactivity once again.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -