gpt4 book ai didi

android - 回到上一个 TabActivity

转载 作者:太空狗 更新时间:2023-10-29 14:31:52 26 4
gpt4 key购买 nike

我该如何解释我的问题?

我有一个使用选项卡的应用程序(通过 Intent ,而不是 View )。我已经在每个 View 内实现了导航,没有任何问题,但我需要在全屏和横向模式(因此没有标签)的 Activity 之一内从 ListView 启动视频播放器。但是当我想播放播放器时(通过按回或调用 finish();),我的应用程序关闭(LogCat 中没有异常)并返回到主屏幕。所以我需要的是当我退出全屏 Activity (为播放器启动)时返回到我的应用程序,其中包含选项卡。

这是我的代码:

//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();
}
});
}


}

这是我的 list :

<?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 标志的位置有关。你怎么看?

最佳答案

已编辑

/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();
}
});
}

总结:

您必须调用 Activity 组的 repalaceView 和 back 方法。当您想要开始新 Activity 时使用 replaceView() 并且当您想要完成您的 Activity 时使用 back()

关于android - 回到上一个 TabActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5841950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com