I'm using fragment.replace to lunch a list of user in a chat application using parse and sinch,at the begin i'm using views but now I'm changing to fragments, but the place just append instead of replace the fragment. I'm using android.support.v4.app.Fragment,FragmentManager and FragmentTransaction. What am I doing wrong . Here's my class:
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.paul.mapasxavo.ListUsersActivity;
import com.example.paul.mapasxavo.R;
public class ChatActivity extends Fragment {
private Button signUpButton;
private Button loginButton;
private EditText usernameField;
private EditText passwordField;
private String username;
private String password;
private Intent intent;
private Intent serviceIntent;
Context thiscontext;
private final String TAG="ChatActivity";
public ChatActivity(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (currentUser != null) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_chat, container, false);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton login");
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
});
}
});
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(username);
user.setPassword(password);
user.signUpInBackground(new SignUpCallback() {
public void done(com.parse.ParseException e) {
if (e == null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton registrar");
} else {
Toast.makeText(getActivity(),
getString(R.string.sigin_error)
, Toast.LENGTH_LONG).show();
}
}
});
}
});
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
@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);
}
}
My fragment is this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ChatActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="@+id/listados"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/txt_usuario"
android:id="@+id/loginUsernameText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="text"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<requestFocus/>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/txt_password"
android:id="@+id/loginPasswordText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="40dp"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
android:layout_gravity="center">
<Button
style="@style/botones"
android:id="@+id/loginButton"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:text="Login"
/>
<Button
style="@style/botones"
android:id="@+id/signupButton"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="Registrar"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
My replace fragment is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ListUsersActivity">
<Button
style="@style/botones"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/logout"
android:id="@+id/logoutButton"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:padding="15dp"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/colorPrimary"
android:id="@+id/usersListView">
</ListView>
</LinearLayout>
Please give a hint. Thanks a lot
Searchin inside the forum, found some anwers, based on this [link][ Android Replace Fragment within a Fragment . I could make it work making some changes in fragment transaction and xml.
Chat Activity:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
//start transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListUsersActivity listUsersActivity = new ListUsersActivity();
fragmentTransaction.addToBackStack("xyz"); fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
serviceIntent = new Intent(getActivity().getApplicationContext(), MessageService.class);
getActivity().startService(serviceIntent);
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
);
}
});
activity_chat.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/listados"
tools:context="com.example.paul.mapasxavo.ChatActivity">
Now it show as aa new layer over the last one.
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.