嗨,我有两个片段活动?主要活动启动第二个活动?现在我经常在这些之间切换两次。
在正常活动中这是不可能的。如果我从第二个活动切换到主要活动,第二个活动将从堆栈中弹出并被销毁。这里有片段活动,这可能吗?如果是这样,如何实现这一目标?
在启动主要活动并按下主要活动上的按钮后,我启动了第二个片段活动。然后我已经使用android手机上的后退键移动到主要片段活动,但现在我想回到我通过按下主要活动上的按钮启动的第二个片段活动。请帮忙?
以下是我的代码:-
以下是主文件。
<application android:allowBackup="true" >
<activity
android:theme="@android:style/Theme.Dialog"
android:name="com.ymd.ipmsg.main.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="@android:style/Theme.Dialog"
android:name="com.ymd.ipmsg.activities.ChatActivity"
android:label="@string/title_activity_chat"
android:parentActivityName="android.support.v4.app.FragmentActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.support.v4.app.FragmentActivity" />
</activity>
</application>
以下是我的activity_chat.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment android:name="com.ymd.ipmsg.fragments.DeviceFragmentTB"
android:id="@+id/tb_fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp"/>
</LinearLayout>
以下是我的activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment android:name="com.ymd.ipmsg.fragments.DeviceFragmentTB"
android:id="@+id/tb_fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp"/>
</LinearLayout>
以下是我的 device_frag.ml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/device_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/device_name"
android:textIsSelectable="true"
android:tag="name" />
<TextView
android:id="@+id/device"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/device_type"
android:textIsSelectable="true"
android:tag="device"/>
<TextView
android:id="@+id/ipadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/device_ip"
android:textIsSelectable="true"
android:tag="ipadd" />
</LinearLayout>
下面是我的 DeviceFragmentTB.java
package com.ymd.ipmsg.fragments;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;
import com.ymd.ipmsg.activities.ChatActivity;
import com.ymd.ipmsg.main.R;
public class DeviceFragmentTB extends Fragment {
private Boolean flag=true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.device_frag, container, false);
TextView nameView = (TextView)view.findViewById(R.id.name);
nameView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),ChatActivity.class);
startActivity(intent) ;
}
});
view.findViewById(R.id.device_layout).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(flag){
View deviceLayout = getView().findViewById(R.id.device_layout);
int deviceFragWidth=deviceLayout.getWidth();
int eachViewWidth=deviceFragWidth/3;
TextView nameView = (TextView)getView().findViewById(R.id.name);
nameView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
nameView.setTextSize(25);
nameView.setBackgroundColor(Color.DKGRAY);
nameView.setTextColor(Color.RED);
nameView.setWidth(eachViewWidth);
TextView deviceView = (TextView)getView().findViewById(R.id.device);
deviceView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
deviceView.setTextSize(25);
deviceView.setTextColor(Color.RED);
deviceView.setBackgroundColor(Color.DKGRAY);
deviceView.setWidth(eachViewWidth);
TextView ipaddView = (TextView)getView().findViewById(R.id.ipadd);
ipaddView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
ipaddView.setTextSize(25);
ipaddView.setBackgroundColor(Color.DKGRAY);
ipaddView.setTextColor(Color.RED);
ipaddView.setWidth(eachViewWidth);
flag=false;
System.out.println("ok sir");
}
}
});
return view;
}
}
以下是我的 MainActivity.java
package com.ymd.ipmsg.main;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ymd.ipmsg.activities.ChatActivity;
import com.ymd.ipmsg.fragments.DeviceFragment;
public class MainActivity extends FragmentActivity implements DeviceFragment.DeviceFragmentListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Show the Up button in the action bar.
// setupActionBar();
}
@Override
public void onDeviceSelected(View view){
String ip=null;
if(((String)view.getTag()).equalsIgnoreCase("ipadd")){
TextView tv=(TextView)view;
ip=tv.getText().toString();
}else{
LinearLayout llo=(LinearLayout)view.getParent();
TextView tv= (TextView)llo.getChildAt(2);
ip=tv.getText().toString();
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
Window win=getWindow();
WindowManager.LayoutParams wlp=win.getAttributes();
wlp.gravity=Gravity.LEFT | Gravity.TOP;
wlp.x=10;
wlp.y=10;
wlp.width=200;
wlp.height=400;
win.setAttributes(wlp);
}
}
以下是我的聊天活动 ChatActiviy.java
package com.ymd.ipmsg.activities;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import com.ymd.ipmsg.main.R;
public class ChatActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
// Show the Up button in the action bar.
//setupActionBar();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
Window win=getWindow();
WindowManager.LayoutParams wlp=win.getAttributes();
wlp.gravity=Gravity.LEFT | Gravity.TOP;
wlp.x=10;
wlp.y=450;
wlp.width=200;
wlp.height=400;
win.setAttributes(wlp);
}
}