每次我将片段附加到我的活动时,我都想注册它以接收活动中的事件.这很容易,因为我可以覆盖FragmentActivity#onAttachFragment(Fragment).从活动中删除片段后,我想从接收事件中取消注册.我希望有一个onDetachFragment我可以以类似方式使用的事件,但我找不到它.
还有另一种方法可以完成我想要做的事情吗?我想在活动中保持注册/取消注册,而不是将其移动到基本片段类(我可以使用onAttach/ onDetach).
在Android文档中,我单独找到了Activity生命周期和Fragment生命周期的规范,但从未在一起.这似乎并不明显,因为我将调试器连接到FragmentActivity我的片段主机,生命周期不仅仅是疯狂.看起来活动首先完成然后片段开始,这是不可能的.
Fragment的生命周期

活动的生命周期

从逻辑上讲,片段应该"跳入"活动的生命周期onResume,它将在活动之前结束onPause,但似乎没有发生.
有人可以向我展示与其父级活动相关的片段的生命周期,还是指导我一些关于此的好教程?
android android-lifecycle android-fragments android-fragmentactivity
在我扩展FragmentActivity类的活动中,我无法使用禁用标题this.requestWindowFeature(Window.FEATURE_NO_TITLE);.它给出了一个ANR.
如何禁用FragmentActivity的标题?
这是活动开始的部分代码:
public class NewOrderActivity extends FragmentActivity implements TabListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:答案:
好吧,我发现在一个已经在其中声明了ActionBar的活动中,标题是Action Bar的一部分而不是windows本身.
所以在我的代码中,我这样做是为了摆脱窗口(或更好地说,ActionBar的)标题:
...
...
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(Window.FEATURE_NO_TITLE);
...
...
Run Code Online (Sandbox Code Playgroud) 我正在阅读《Android 编程》,书中说:
完全有可能在 onCreate 活动仍与先前创建的片段相关联时调用它。只要在调用 onCreate 方法时简单地添加一个新片段就会泄漏片段。为了防止这种情况,示例代码使用了片段管理器标记和位置功能。
代码是:
super.onCreate( state);
setContentView( R.layout.main);
FragmentManager fragMgr = getFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
if (null = = fragMgr.findFragmentByTag( FRAG1_TAG)) {
xact.add( R.id.date_time, new DateTime(), FRAG1_TAG);
}
xact.commit();
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么在 onCreate 中需要这样做吗?
我认为片段生命周期总是依赖于活动的生命周期,并且活动中的 onCreate 总是在活动被创建时被调用(即它总是死的)。
因此,如果片段生命周期与活动相关联,那么当活动死亡时所有片段不应该死亡,因此在活动中调用 onCreate 时片段将始终为空吗?
是否有例外或有人可以解释为什么我的想法不正确(我实际上认为它不正确但不知道为什么?)
android android-fragments android-activity android-fragmentactivity
我正在使用片段和..
我的代码中遇到了一个我无法找到的问题.
logcat指向这段代码,在我的一个片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)
我的主要类(FragmentActivity):
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class Fragments extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
MainActivity fragment = new MainActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment);
transaction.commit();
}
public void onSelectFragment(View view) {
Fragment newFragment;
if (view == findViewById(R.id.add)) {
newFragment = new Add();
} else if (view == …Run Code Online (Sandbox Code Playgroud) android class android-fragments layout-inflater android-fragmentactivity
我有一个片段:
ProductsFragments extends Fragment
Run Code Online (Sandbox Code Playgroud)
和一个活动
AdminMenuActivity extends ActionBarActivity
Run Code Online (Sandbox Code Playgroud)
我想从AdminMenuActivity调用ProductsFragments.我使用了2个选项:
1)
FragmentManager fm = getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.frame_layout, android.support.v4.app.Fragment.instantiate(AdminMenuActivity.this, fragments[1]));
tx.commit();
Run Code Online (Sandbox Code Playgroud)
2)
Intent intent1 = new Intent(AdminMenuActivity.this, ProductsActivity.class);
startActivity(intent1);
Run Code Online (Sandbox Code Playgroud)
两者都失败了.我不想用FragmentActivity扩展ProductsFragments,因为它没有给我supportActionBar v7
那我怎么打电话Fragment?
我有一个需要在活动上方显示的片段,但活动中的按钮一直显示在该片段上方。我需要该片段与活动完全重叠。
活动的xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:lines="3"
android:maxLines="10"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
片段
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<TextView
android:id="@+id/firstnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
/>
<TextView
android:id="@+id/lastnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat" />
<fragment
android:id="@+id/fragment_chat"
android:name="user.InitiateChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
private void init() { …Run Code Online (Sandbox Code Playgroud) xml android android-layout android-fragments android-fragmentactivity
如果我有一个扩展基本活动BA的活动A,那么我能够安全地从活动A访问活动BA中的任何变量.我现在使用的是包含片段F的活动A.现在从这个片段我想要以同样的方式访问A的所有变量,就像我上面所做的那样,如果没有,除了通过公共方法使其可用之外,还有一种安全的方法.
或者有没有办法可以将基本活动中的变量复制到基本片段,以便在所有活动和片段中都可用.
java android android-fragments android-activity android-fragmentactivity
我想在多个活动中使用一个片段。在我将在第一个活动中使用它,我创建了它
final ScoreBoard fragment = new ScoreBoard();
getFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
Run Code Online (Sandbox Code Playgroud)
在第二个活动中,我在 onCreate() 方法中放置了相同的代码。但是,即使我已经通过 onSavedInstanceState() 和 onActivityCreated() 保存了它们,片段也会不断重置并且不会将其值保留在第二个活动中。我是在重新创建片段并重置它吗?谢谢你。
android-fragments android-activity android-fragmentactivity onactivityresult onsaveinstancestate
我正在尝试从 backstack 中检索片段。它正在被检索,但问题是在按下后退按钮时,oncreate 视图和当前片段的后续生命周期方法也被调用。这是我将片段放入 backstack 的代码:
fragment=new FileTrackingFragment();
bundle=new Bundle();
bundle.putString("name","Dak Monitoring");
bundle.putInt("num",2);
fragment.setArguments(bundle);
ft.replace(R.id.parent, fragment, fragment.getClass().getName());
ft.addToBackStack(fragment.getClass().getName());
ft.commit();
break;
Run Code Online (Sandbox Code Playgroud)
这是片段的代码:
package com.example.rajvirsingh.epunjaboffice.DakMonitoring;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rajvirsingh.epunjaboffice.FileTracking.HistoryAdapter;
import com.example.rajvirsingh.epunjaboffice.MainActivity;
import com.example.rajvirsingh.epunjaboffice.R;
import com.example.rajvirsingh.epunjaboffice.Utility.Constants;
import com.example.rajvirsingh.epunjaboffice.Utility.SessionManager;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.json.JSONArray;
import java.util.ArrayList;
import …Run Code Online (Sandbox Code Playgroud) android android-fragments android-fragmentactivity fragment-lifecycle fragment-backstack