我如何解释我的问题?
我有一个使用标签的应用程序(通过Intents,而不是视图)。我已经在每个视图中实现了导航而没有遇到任何麻烦,但是我需要从全屏和横向模式中的一个Activity中的ListView中启动视频播放器(所以没有标签)。但是当我想要播放播放器时(通过按回或调用finish();),我的应用程序将关闭(LogCat中没有例外)并返回主屏幕。 所以我需要的是当我退出全屏活动(为播放器启动)时,回到我的应用程序,与选项卡一起。
这是我的代码:
//Main activity :
package com.ccn;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class CCNMain extends TabActivity {
public static TabHost tabHost;
static CCNMain myActivity = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myActivity = this;
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec; // reusable tabspec for each tab
Intent intent;
intent = new Intent().setClass(this, NewsGroup.class);
spec = tabHost.newTabSpec("nnews")
.setIndicator("A la une",res.getDrawable(R.drawable.home))
.setContent(intent);
tabHost.addTab(spec);
/*intent = new Intent().setClass(this, ArticlesGroup.class);
spec = tabHost.newTabSpec("aarticles")
.setIndicator("Articles",res.getDrawable(R.drawable.articles))
.setContent(intent);
tabHost.addTab(spec);*/
intent = new Intent().setClass(getApplicationContext(), ContactGroup.class);
spec = tabHost.newTabSpec("vvideos")
.setIndicator("Vidéos",res.getDrawable(R.drawable.video))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(getApplicationContext(), ContactGroup.class);
spec = tabHost.newTabSpec("ccontact")
.setIndicator("Contact",res.getDrawable(R.drawable.contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
//ContactGroup :
package com.ccn;
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class ContactGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static ContactGroup groupContact;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
groupContact = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("idHoraireActivity", new
Intent(this,ContactActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if(history.size() > 1) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}else {
finish();
}
}
//@Override
public void onBackPressed() {
ContactGroup.groupContact.back();
return;
}
}
//ContactActivity:
package com.ccn;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class ContactActivity extends Activity {
static ContactActivity bidule = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos);
bidule = this;
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(getApplicationContext(), videoPlayerActivity.class);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
bidule.startActivity(i);
ContactActivity.this.startActivityForResult(i, 0);
}
});
}
}
//VideoPlayerActivity:
package com.ccn;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoPlayerActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos);
//the VideoView will hold the video
String url = // Video url;
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
在这里我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ccn"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CCNMain"
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="ArticlesGroup"></activity>
<activity android:name="ContactGroup"></activity>
<activity android:name="NewsGroup"></activity>
<activity android:name="NewsActivity"></activity>
<activity android:name="ArticlesAvtivity"></activity>
<activity android:name="ContactActivity"></activity>
<activity android:name="videoPlayerActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape"></activity>
</application>
</manifest>
感谢您的帮助。 我的猜测:它与我调用startActivity和Intent标志的地方有关。 你觉得怎么样?
答案 0 :(得分:1)
<强>被修改强>
/Main activity :
package com.ccn;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class CCNMain extends TabActivity {
public static TabHost tabHost;
static CCNMain myActivity = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myActivity = this;
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec; // reusable tabspec for each tab
Intent intent;
intent = new Intent().setClass(this, NewsGroup.class);
spec = tabHost.newTabSpec("nnews")
.setIndicator("A la une",res.getDrawable(R.drawable.home))
.setContent(intent);
tabHost.addTab(spec);
/*intent = new Intent().setClass(this, ArticlesGroup.class);
spec = tabHost.newTabSpec("aarticles")
.setIndicator("Articles",res.getDrawable(R.drawable.articles))
.setContent(intent);
tabHost.addTab(spec);*/
intent = new Intent().setClass(getApplicationContext(), ContactGroup.class);
spec = tabHost.newTabSpec("vvideos")
.setIndicator("Vidéos",res.getDrawable(R.drawable.video))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(getApplicationContext(), ContactGroup.class);
spec = tabHost.newTabSpec("ccontact")
.setIndicator("Contact",res.getDrawable(R.drawable.contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
//ContactGroup :
package com.ccn;
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class ContactGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static ContactGroup groupContact;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
groupContact = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("idHoraireActivity", new
Intent(this,ContactActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if(history.size() > 1) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}else {
finish();
}
}
//@Override
public void onBackPressed() {
ContactGroup.groupContact.back();
return;
}
}
//ContactActivity:
package com.ccn;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class ContactActivity extends Activity {
//static ContactActivity bidule = null;
ContactGroup contactGroup = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos);
//bidule = this;
contactGroup = new ContactGroup();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
View view = getLocalActivityManager().startActivity("videoPlayerActivity", new
Intent(this,videoPlayerActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
contactGroup.replaceView(view);
}
});
}
}
//VideoPlayerActivity:
package com.ccn;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoPlayerActivity extends Activity {
ContactGroup contactGroup = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos);
ContactGroup contactGroup = new ContactGroup();
//the VideoView will hold the video
String url = // Video url;
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
contactGroup.back();
}
});
}
摘要:
您必须调用您的活动组的repalaceView和back方法。当您想要开始新活动时使用replaceView()
,当您想要完成活动时,请使用back()
答案 1 :(得分:0)
我已经实施了同样的案例。但是没有使用ActivityGroup。在测试中,很明显它的反应并不防水。当我启动Mediaplayer活动并在堆栈中放入新活动时,操作系统正确处理后台事件。
public class VideoActivity extends Activity {
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mListView = (ListView)findViewById(R.id.list);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent().setClass(view.getContext(), ViewVideoActivity.class);
startActivity(intent);
}
});
}
VieoVideoActivity是横向强制的全屏活动,没有重写的关键事件实现。
答案 2 :(得分:0)
我希望我能正确理解你的查询(请纠正我)。 TabActivity中有多个选项卡,除了一个与另一个Activity相关联的选项卡外,其中一个选项卡应位于TabActivity的“内部”。对于视频,您希望它在TabActivity的相同级别(不在内部)启动。这样系统就可以处理VideoActivity的后台事件。
| |
| |
|T (1)| <- tab activity with tab#1
-------
选择标签#2:
| |
| |
|T (2)| <- tab activity with tab#2
-------
选择标签#video:
| |
|Video| <- video activity
|T (2)| <- tab activity with tab#2
-------
由于您可以毫无问题地创建“子活动”,这里我只是向您展示如何创建外部活动意图:
private void createExternalTab(Class clazz, String tag, String label, int iconResId) {
// intent to launch the activity externally
final Intent intent = new Intent(this, clazz);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
tabHost.addTab(tabHost.newTabSpec(tag)
.setContent(R.id.dummy)
.setIndicator(label, iconResId));
// find the recently added tab
TabWidget w = tabHost.getTabWidget();
View view = w.getChildAt(w.getChildCount() - 1);
// here is the magic!
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
以下是TabActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/dummy"
android:layout_width="0dp"
android:layout_height="0dp"/>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:showDividers="none"/>
</RelativeLayout>
</TabHost>
注1:我的代码适用于FragmentActivity
注意2:这里的关键点是:将标签内容映射到R.id.tabcontent中的不可见元素。然后修改选项卡的onClickListener,以便在TabActivity下调用Intent,而不是由TabWidget / TabHost启动。