小编Tün*_*nde的帖子

什么时候onRestoreInstanceState调用?

对不起我的不解,但我是android开发的新手.

我有一个活动A和活动B的应用程序,我从活动A转到活动B.当我离开活动A时,onSaveInstanceState方法被调用,但是当我回到活动A时(来自活动B在同一个应用程序中) ),onCreate方法中的bundle 为null.

我该怎么办,以保存活动A以前的状态?我只想存储应用程序生命周期的数据.

有人可以帮我弄这个吗?

这是我的活动A的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null)
    {
        Log.v("Main", savedInstanceState.getString("test"));
    }
    else
    {
        Log.v("Main", "old instance");
    }
}  

@Override
public void onSaveInstanceState(Bundle savedInstanceState) 
{
    Log.v("Main", "save instance");

    savedInstanceState.putString("test", "my test string");

    super.onSaveInstanceState(savedInstanceState);
}


public void buttonClick(View view)
{
    Intent intent = new Intent(this, Activity2.class);
    startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

这是我的活动B的代码,当我按下按钮返回活动A:

public void onBack(View view)
{
    NavUtils.navigateUpFromSameTask(this);
}
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle onrestoreinstancestate onsaveinstancestate

38
推荐指数
2
解决办法
4万
查看次数

如何以编程方式获取服务器的证书并添加到truestore,并检查证书

在我的 Android 应用程序中,我想使用 https 连接到使用自签名证书的用户指定服务器。因为https服务器是用户指定的,所以我之前不知道服务器的证书,因此我想:

  • 动态获取服务器的证书
  • 将此证书的公钥添加到应用程序的信任存储中
  • 验证服务器

我不想在用户不检查证书的情况下简单地接受每个自签名证书

我在第一步中遇到了困难,有人可以向我展示一个带有基本说明的工作示例吗?任何提示表示赞赏。多谢

ssl https android ssl-certificate

5
推荐指数
1
解决办法
1507
查看次数

Android通知无法在Android 6.0(Marshmallow)上运行

我正在Android上实现本地通知,我遇到的问题是它们没有出现在Android 6.0(三星S7)上.我正在寻找解决方案,但我找不到任何解决这个问题的方法.我在正确的res/drawable文件夹中有图标,我也定义了通知标题,文本,铃声(原始文件夹)但它没有显示...有我的代码:

    Context acontext = getApplicationContext();

    PackageManager pm = acontext.getPackageManager();
    Intent notificationIntent = pm.getLaunchIntentForPackage(acontext.getPackageName());
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(acontext, 0, notificationIntent, 0);
    int notification_icon = acontext.getResources().getIdentifier("icon", "drawable", acontext.getPackageName());
    int notificationID = 0;

    // Build notification
    Notification noti = new Notification.Builder(acontext)
            .setContentTitle("Title")
            .setContentText("Incoming text")
            .setSmallIcon(notification_icon)
            .setContentIntent(pendingIntent)
            .setLights(Color.RED, 1, 1)
            .build();
    NotificationManager notificationManager = (NotificationManager) acontext.getSystemService(Context.NOTIFICATION_SERVICE);
    // hide the notification after its selected
    noti.sound = Uri.parse("android.resource://" + acontext.getPackageName() + "/raw/incoming");
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, noti);
Run Code Online (Sandbox Code Playgroud)

有没有其他人遇到过这个问题?任何帮助,将不胜感激.谢谢.

notifications android android-notifications android-6.0-marshmallow

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

在android中设置单选AlertDialog默认项

我使用以下代码来创建AlertDialog

    CharSequence[] array = {"Font1", "Font2", "Font3", "Font4"};
    callback = (DialogClickListener) fragment;

    builder = new AlertDialog.Builder(context); 
    builder.setTitle("Font Settings")
            .setSingleChoiceItems(array, 0, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    selected = array[arg1].toString();
                }
            })
           .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
              }
          })
          .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                callback.onYesClick(selected);
            }
        });
Run Code Online (Sandbox Code Playgroud)

如何设置默认选择的元素?请帮忙 :)

android android-alertdialog

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

在android中使用的颜色选择器

有人可以提供一些颜色选择器的代码,它在Android中的用途?

我在这里找到了一些Android Color Picker的例子,但我不知道如何使用它:(

请帮帮我,谢谢

android

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

在android中运行时更改按钮背景

我想在RUNTIME更改我的按钮背景

我知道如何为按钮添加自定义背景,但是

android android-button

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