标签: lifecycle

Android活动生命周期 - 这些方法有哪些?

Android活动的生命周期是什么?为什么这么多的发音相似的方法(onCreate(),onStart(),onResume()初始化过程调用),和许多其他人(onPause(),onStop(),onDestroy())呼吁在结束了吗?

这些方法何时被调用,它们应该如何正确使用?

lifecycle android onresume oncreate ondestroy

406
推荐指数
7
解决办法
28万
查看次数

希望了解iOS UIViewController的生命周期

你能解释一下管理UIViewController生命周期的正确方法吗?

我特别想知道如何使用Initialize,ViewDidLoad,ViewWillAppear,ViewDidAppear,ViewWillDisappear,ViewDidDisappear,ViewDidUnloadDispose在单触摸的方法UIViewController类.

lifecycle uiviewcontroller xamarin.ios ios

280
推荐指数
10
解决办法
23万
查看次数

如何检索视图的尺寸?

我有一个观点TableLayout, TableRow and TextView.我希望它看起来像一个网格.我需要得到这个网格的高度和宽度.这些方法getHeight()getWidth()总是返回0.这发生在我和动态也格式化网格时我使用XML版本.

如何检索视图的尺寸?


这是我在Debug中用来检查结果的测试程序:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TextView;

public class appwig extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.maindemo);  //<- includes the grid called "board"
      int vh = 0;   
      int vw = 0;

      //Test-1 used the xml layout (which is displayed on the screen):
      TableLayout tl = (TableLayout) findViewById(R.id.board);  
      tl = (TableLayout) findViewById(R.id.board);
      vh = tl.getHeight();     //<- getHeight returned 0, Why?  
      vw = tl.getWidth();     //<- …
Run Code Online (Sandbox Code Playgroud)

layout lifecycle android android-widget measure

207
推荐指数
6
解决办法
13万
查看次数

onNewIntent()生命周期和已注册的侦听器

我正在使用singleTop Activity来通过搜索对话框接收意图onNewIntent().

我注意到的是onPause()之前调用过onNewIntent(),然后调用它onResume().视觉:

  • 搜索对话框已启动
  • 搜索意图触发了活动
  • onPause()
  • onNewIntent()
  • onResume()

问题是我有注册的监听器onResume()被删除onPause(),但在onNewIntent()通话中需要它们.有没有一种标准方法可以让这些听众可用?

lifecycle android android-intent

140
推荐指数
2
解决办法
9万
查看次数

什么决定了Dagger 2中组件(对象图)的生命周期?

我试图围绕Dagger 2中的范围,特别是范围图的生命周期.如何创建一个在离开示波器时将被清理的组件.

对于Android应用程序,使用Dagger 1.x,您通常在应用程序级别具有根范围,您可以扩展该范围以在活动级别创建子范围.

public class MyActivity {

    private ObjectGraph mGraph;

    public void onCreate() {
        mGraph = ((MyApp) getApplicationContext())
            .getObjectGraph()
            .plus(new ActivityModule())
            .inject(this);
    }

    public void onDestroy() {
        mGraph = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

只要您保留对它的引用,子范围就存在,在这种情况下,它是您的Activity的生命周期.删除onDestroy中的引用可确保范围图可以自由地进行垃圾回收.

编辑

杰西威尔逊最近发布了一个mea culpa

Dagger 1.0严重搞砸了它的范围名称...... @Singleton注释用于根图和自定义图形,因此弄清楚事物的实际范围是很棘手的.

我读过/听过的其他一切都指向Dagger 2改进了范围的工作方式,但我很难理解其中的差异.根据@Kirill Boyarshinov在下面的评论,组件或依赖关系的生命周期仍然像往常一样通过具体的引用来确定.那么Dagger 1.x和2.0范围之间的差异纯粹是语义清晰度的问题吗?

我的理解

匕首1.x

依赖性是否是@Singleton.根图和子图中的依赖性同样如此,导致依赖关系绑定到哪个图形的模糊性(参见In Dagger是缓存的子图中的单例,或者当新的活动子图时它们总是被重新创建)是构造?)

匕首2.0

自定义范围允许您创建语义清晰的范围,但在功能上等同于@Singleton在Dagger 1.x中应用.

// Application level
@Singleton
@Component( modules = MyAppModule.class )
public interface MyAppComponent {
    void inject(Application app);
}

@Module
public class MyAppModule { …
Run Code Online (Sandbox Code Playgroud)

java lifecycle dagger-2

126
推荐指数
1
解决办法
2万
查看次数

如何使用生命周期方法getDerivedStateFromProps而不是componentWillReceiveProps

它似乎componentWillReceiveProps将在即将发布的版本中完全淘汰,支持新的生命周期方法getDerivedStateFromProps.

https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

经过检查,看起来你现在无法在this.propsnextProps你之间进行直接比较componentWillReceiveProps.有没有办法解决?

此外,它现在返回一个对象.我是否正确地假设返回值基本上是this.setState

以下是我在网上找到的一个例子 https://github.com/reactjs/rfcs/blob/master/text/0006-static-lifecycle-methods.md#state-derived-from-propsstate

之前

class ExampleComponent extends React.Component {
  state = {
    derivedData: computeDerivedState(this.props)
  };

  componentWillReceiveProps(nextProps) {
    if (this.props.someValue !== nextProps.someValue) {
      this.setState({
        derivedData: computeDerivedState(nextProps)
      });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

class ExampleComponent extends React.Component {
  // Initialize state in constructor,
  // Or with a property initializer.
  state = {};

  static getDerivedStateFromProps(nextProps, prevState) {
    if (prevState.someMirroredValue !== nextProps.someValue) {
      return {
        derivedData: computeDerivedState(nextProps),
        someMirroredValue: nextProps.someValue
      }; …
Run Code Online (Sandbox Code Playgroud)

javascript lifecycle reactjs

125
推荐指数
3
解决办法
10万
查看次数

116
推荐指数
6
解决办法
8万
查看次数

片段生命周期 - 在显示/隐藏时调用哪个方法?

我使用以下方法通过显示/隐藏它们来切换片段(在我的NavigationDrawer中).

protected void showFragment(int container, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ( lastTag != null && !lastTag.equals("")) {
            Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
            if ( lastFragment != null ) {
                transaction.hide( lastFragment );
            }
        }

        if ( fragment.isAdded() ) {
            transaction.show( fragment );
        }
        else {
            transaction.add( container, fragment, tag );
        }

        if ( addToBackStack ) {
            transaction.addToBackStack( tag );
        }

        transaction.commit();

        // set the active …
Run Code Online (Sandbox Code Playgroud)

lifecycle android android-fragments fragmenttransaction

95
推荐指数
4
解决办法
7万
查看次数

Android应用程序内存不足问题 - 尝试了一切但仍然不知所措

我花了整整4天时间尽我所能来弄清楚我正在开发的应用程序中的内存泄漏,但事情很久以前就停止了.

我正在开发的应用程序具有社交性,因此请考虑配置文件活动(P)并列出包含数据的活动 - 例如徽章(B).您可以从配置文件跳转到徽章列表,转到其他配置文件,其他列表等.

所以想象一下这样的流程P1 - > B1 - > P2 - > B2 - > P3 - > B3等.为了保持一致性,我正在加载同一个用户的配置文件和徽章,所以每个P页面是相同的,所以是每个B页面.

这个问题的一般要点是:在导航了一下之后,根据每个页面的大小,我在随机位置得到了一个内存不足的例外 - 位图,字符串等 - 它似乎并不一致.

在做了一切可以想象的事情来弄清楚为什么我的内存不足之后,我什么也没想出来.我不明白的是,如果Android在加载时耗尽内存而不是崩溃,那么为什么Android不会杀死P1,B1等.如果我通过onCreate()和onRestoreInstanceState()返回它们,我会期望这些早期活动会死亡并复活.

更别说这个 - 即使我做P1 - > B1 - >返回 - > B1 - >返回 - > B1,我仍然会崩溃.这表明某种内存泄漏,即使在转储hprof并使用MAT和JProfiler后,我也无法查明它.

我已禁用从Web加载图像(并增加了加载的测试数据以弥补它并使测试公平)并确保图像缓存使用SoftReferences.Android实际上试图释放它所拥有的一些SoftReferences,但就在它崩溃内存之前.

徽章页面从Web获取数据,从BaseAdapter将其加载到EntityData数组中并将其提供给ListView(我实际上使用的是CommonsWare的优秀MergeAdapter,但在此Badge活动中,实际上只有1个适配器,但我无论哪种方式都想提到这个事实).

我已经完成了代码并且无法找到任何可能泄漏的内容.我清除并取消了我能找到的所有内容,甚至是System.gc()左右,但应用程序崩溃了.

我仍然不明白为什么堆栈上的非活动活动不会被收获,我真的很想知道这一点.

在这一点上,我正在寻找任何提示,建议,解决方案......任何有用的东西.

谢谢.

lifecycle android out-of-memory android-activity

87
推荐指数
3
解决办法
4万
查看次数

什么是iPhone应用程序的生命周期?

谁能解释一下iPhone的生命周期过程是什么?即从应用程序生命周期的开始到结束.

lifecycle ios

84
推荐指数
3
解决办法
8万
查看次数