21

我的应用程序通常工作得很好,直到我在特定设备上遇到一个奇怪的问题。App中有2个活动。在 ActivityA 内启动 ActivityB 后,ActivityA 启动没有问题。但是,在我返回 ActivityA 并按下硬件按钮或调用 finish(); 在 ActivityB 的 closeButton 内部,ActivityA 会重新加载自身。它再次触发 onCreate() 并重新加载其所有内容。而且我不会改变手机的方向。这种奇怪的行为只出现在超过 1.000 次下载应用程序的 15 部手机中。

此问题仅在 Galaxy S3 Android OS 4.1.2 上出现。而这也很奇怪。

你知道为什么会这样吗?

当我在按钮侦听器中启动一个新 Activity 时,如下所示:

ActivityA.java (MesajlarListViewActivity)

    public class MesajlarListViewActivity extends TrackedActivity {

    Context context = null;

    // contacts JSONArray
    JSONArray contacts = null;

    ArrayList<Message> productArray = new ArrayList<Message>();

    private ProductAdapter adapter;
    private ListView productList;
    private Runnable viewOrders;
    private HoloProgressIndicator profilInfoProgress = null;

    ImageView kapatButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mesajlar_list);

        context = this;

        kapatButton = (ImageView) findViewById(R.id.kapat_button);
        /* kapat button onclick listener. */
        // =================================================================================================================
        kapatButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)
            {
                // Set vibration on touch.
                KnetGenericClass.vibratePhone(context);

                finish();
            }

        });
        // =================================================================================================================
        //Progress bar.
        profilInfoProgress = (HoloProgressIndicator) findViewById(R.id.profil_info_progress);

        // cheking internet connectivity.
        if(KnetGenericClass.checkInternetConnection(context))
        {
            // start task!
            /* internet var ise web service baglantisi kurmaya baslayabiliriz. */
            startActivityIndicatorWithThread();
        }
        else
        {
            KnetGenericClass.printErrorMessage(context, "Bağlantı Hatası",
                    "Lütfen internet bağlantınızı kontrol ediniz.");
        }

        productList = (ListView) findViewById(R.id.product_list);
        adapter = new ProductAdapter(this, R.layout.message_row, productArray);
        productList.setAdapter(adapter);

        // When user click a view on list view new page is appearing.
        productList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {

                // Set vibration on touch.
                KnetGenericClass.vibratePhone(context);

                /* Navigate to message detay activity class with ilan ID. */
                Intent myIntent = new Intent(view.getContext(), MesajDetayActivity.class);
                myIntent.putExtra("messageID", productArray.get(position).getId());
                startActivity(myIntent);

                // setting image of clicked message null.
                RelativeLayout relativeLayout = (RelativeLayout) view;
                ImageView unreadedImageView = (ImageView) relativeLayout.findViewById(R.id.unreaded_image);
                unreadedImageView.setImageResource(0);
            }
        });
    }

    public class ProductAdapter extends ArrayAdapter<Message> {
        ArrayList<Message> items;

        public ProductAdapter(Context context, int textViewResourceId, ArrayList<Message> objects) {
            super(context, textViewResourceId, objects);
            this.items = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if(convertView == null)
            {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.message_row, null);
            }

            ImageView unreadedImageView = (ImageView) convertView.findViewById(R.id.unreaded_image);
            TextView productName = (TextView) convertView.findViewById(R.id.product_name);
            TextView productDetail = (TextView) convertView.findViewById(R.id.product_detail);
            // TextView productDate = (TextView)
            // convertView.findViewById(R.id.product_date);
            TextView sentDate = (TextView) convertView.findViewById(R.id.product_date);

            productName.setText(items.get(position).getSender());
            productDetail.setText(items.get(position).getTitle());
            // String bodyNoHTML = items.get(position).getBody();

            if(items.get(position).getIsReaded())
            {
                unreadedImageView.setImageResource(0);
            }
            else
            {
                unreadedImageView.setImageResource(R.drawable.bluedot);
            }

            String dateStr = items.get(position).getSentDate();
            try
            {
                sentDate.setText(dateStr.substring(6, 8) + "." + dateStr.substring(4, 6) + "." + dateStr.substring(0, 4)
                        +" "+dateStr.substring(8, 10)+":"+dateStr.substring(10, 12));
            }
            catch(Exception e)
            {
                sentDate.setText("");
            }


            return convertView;
        }

    }// @end of product adapter class.

    /* web service'e baglanti kurulan methodu threadin icerisinde cagiriyoruz. */
    public void startActivityIndicatorWithThread()
    {
        // ==============================================================================================
        // getting ilan details into arraylist.
        // setting up thread.
        viewOrders = new Runnable() {
            public void run()
            {
                getMessageListFromWebService();
            }
        };
        Thread thread = new Thread(null, viewOrders, "MagentoBackground");
        thread.start();
        profilInfoProgress.start();
        // ==============================================================================================
        // @end of the thread declaration.
    }

    public void getMessageListFromWebService()
    {
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser(context);

        // getting JSON string from URL
        JSONArray jsonArray = jParser.getAuthorizedInfoFromUrlToJSONArray(
                WebServiceInfo.getKnetWebServiceLink()+"/API/Member/GetInboxMessageList", MainActivity.getAccessToken());

        // if json is null then there is a problem.
        if(jsonArray == null)
        {
            // if json array is null then print error message.
            runOnUiThread(showAlertMessage);
            runOnUiThread(returnRes);
            return;
        }

        try
        {
            // Eger aranilan kritere gore ilan yok ise hata mesaji basiyoruz.
            if(jsonArray.length() == 0)
            {
                // if json array is null then print error message.
                runOnUiThread(showAlertIlanYokMessage);
                runOnUiThread(returnRes);
                return;
            }

            // looping through All Contacts
            for (int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject c = jsonArray.getJSONObject(i);

                // Storing each json item in variable
                // String id = c.getString(TAG_ID);
                String id = c.getString("Id");
                String sender = c.getString("Sender");
                // String body = c.getString("Body");
                String title = c.getString("Title");
                String sentDate = c.getString("SentDate");
                Boolean isReaded = c.getBoolean("IsRead");

                Message productObject = new Message(id, sender, "", title, sentDate, isReaded);
                productArray.add(productObject);
            }
        }
        catch (Exception e)
        {
            Log.e("BACKGROUND_PROC", e.getMessage());
        }
        runOnUiThread(returnRes);
    }


    // @end of thread.
    private Runnable returnRes = new Runnable() {

        public void run()
        {
            profilInfoProgress.stop();
            adapter.notifyDataSetChanged();// refreshing data over adapter in
                                            // list view.
        }
    };

    // @end of thread.
    private Runnable showAlertMessage = new Runnable() {

        public void run()
        {
            // Bu hata genelde linkteki problemden, servera ulasilamamasindan
            // veya timeouttan meydana gelir.
            Toast.makeText(getApplicationContext(),
                    "Mesajlar alınamadı lütfen daha sonra tekrar deneyiniz.", 
                    Toast.LENGTH_LONG).show();
        }
    };

    private Runnable showAlertIlanYokMessage = new Runnable() {

        public void run()
        {
            // Bu hata aranilan kelimeye gore ilan bulunamazsa gelir.
            Toast.makeText(getApplicationContext(),
                    "Mesajlar bulunamadı.", 
                    Toast.LENGTH_LONG).show();
        }
    };

}

==================================================== =======================

ActivityB.java (MesajDetayActivity.java)

public class MesajDetayActivity extends TrackedActivity {

    private HoloProgressIndicator profilInfoProgress = null;

    TextView titleTextView = null;
    TextView senderTextView = null;
    TextView dateTextView = null;
    WebView bodyWebView = null;

    Message messageObject = null;

    String messageID = null;

    ImageView kapatButton = null;

    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mesajdetaylari);

        context = this;

        kapatButton = (ImageView) findViewById(R.id.kapat_button);
        /* kapat button onclick listener. */
        // =================================================================================================================
        kapatButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)
            {
                // Set vibration on touch.
                KnetGenericClass.vibratePhone(context);

                finish();
            }

        });
        // =================================================================================================================
        //Progress bar.
        profilInfoProgress = (HoloProgressIndicator) findViewById(R.id.profil_info_progress);

        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            messageID = extras.getString("messageID");
        }

        titleTextView = (TextView) findViewById(R.id.title_textview);
        senderTextView = (TextView) findViewById(R.id.sender_textview);
        dateTextView = (TextView) findViewById(R.id.date_textview);
        bodyWebView = (WebView) findViewById(R.id.mesaj_webView);

        // Show the ProgressDialog on this thread
        profilInfoProgress.start();

        // Start a new thread that will download all the data
        new MakeItTask().execute();

    }

    // Async task.
    private class MakeItTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args)
        {
            Log.i("MyApp", "Background thread starting");

            // This is where you would do all the work of downloading your data
            // getting message detay
            /* connect to web service */
            getMessageDetayFromWebService();

            return null;
        }

        protected void onPostExecute(Object result)
        {
            // Pass the result data back to the main activity
            // TakipListeActivity.this.data = result;
            try
            {
                titleTextView.setText("Başlık: " + messageObject.getTitle());
                senderTextView.setText("Gönderen: " + messageObject.getSender());
                dateTextView.setText("Tarih: " + messageObject.getSentDate().substring(6, 8) + "."
                        + messageObject.getSentDate().substring(4, 6) + "."
                        + messageObject.getSentDate().substring(0, 4));

                if(!messageObject.getBody().contains("img"))
                {
                    bodyWebView.loadDataWithBaseURL(null, messageObject.getBody(), "text/html", "UTF-8", null);
                }

            }
            catch (Exception e)
            {
                Log.e(CONNECTIVITY_SERVICE, "Mesaj Detayi bilgileri basilamadi.");
            }

            profilInfoProgress.stop();
        }
    }

    /* web service'e baglanti kurulan methodu threadin icerisinde cagiriyoruz. */
    public void getMessageDetayFromWebService()
    {
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser(context);

        // getting JSON string from URL
        JSONObject jsonObject = jParser.getAuthorizedInfoFromUrlToJSONObject(
                WebServiceInfo.getKnetWebServiceLink()+"/API/Member/GetInboxMessage/" + messageID, MainActivity.getAccessToken());

        // if json is null then there is a problem.
        if(jsonObject == null)
        {
            return;
        }

        try
        {
            String title = jsonObject.getString("Title");
            String id = jsonObject.getString("Id");
            String sender = jsonObject.getString("Sender");
            String date = jsonObject.getString("SentDate");
            String body = jsonObject.getString("Body");

            messageObject = new Message(id, sender, body, title, date, true);

        }
        catch (Exception e)
        {
            Log.e("BACKGROUND_PROC", e.getMessage());
        }

    }// @end of getIlanDetayFromWebService.

}

编辑:不仅这两个活动有这个问题,所有活动在某些手机上的行为都相同。

4

13 回答 13

48

检查“设置” > “系统” > “开发人员选项” > “应用程序”下的“不保留活动”是否已启用。

于 2013-03-01T22:57:58.007 回答
11

Activity 文档 ( http://developer.android.com/reference/android/app/Activity.html ) 对后台 Activity 的生命周期进行了如下说明:

后台活动(对用户不可见且已暂停的活动)不再至关重要,因此系统可以安全地终止其进程以回收其他前台或可见进程的内存。如果它的进程需要被杀死,当用户导航回活动(使其再次在屏幕上可见)时,将使用它之前在 onSaveInstanceState(Bundle) 中提供的 savedInstanceState 调用它的 onCreate(Bundle) 方法,以便它可以在与用户上次离开时​​相同的状态下重新启动。

换句话说,ActivityA 可能在 ActivityB 处于活动状态时被操作系统销毁,也可能不会被操作系统销毁,因此必须在代码中处理这种情况。如果 ActivityA 已被销毁,当用户按下 ActivityB 中的返回按钮时,将调用 onCreate(Bundle)。

于 2013-02-21T12:59:11.433 回答
9

有一个名为“不保留活动”的 Android 开发人员设置。此选项的描述是“用户离开后立即销毁所有活动”。这听起来很好地描述了您所看到的内容,并且由于您只在几部手机上看到它,因此认为这是由非默认系统设置引起的想法似乎是合理的。

理想情况下,您的应用程序仍然可以在这种情况下工作,即使不是最理想的。但是,如果此设置对您的应用程序来说是一个问题,您可能希望为您的用户记录此问题。

于 2013-03-01T23:18:43.437 回答
7

您是否尝试过更改launchmodeAndroid Manifest 中的 尝试将此添加到您的 Activity 声明中:

android:launchMode="singleTask"

接下来,尝试使用startActivityForResult, 而不是startActivity. 这将强制 Activity A 在 Activity B 完成时调用其onActivityResult(int, int, Intent)方法——这可能会跳过对onCreate. 然后,在 Activity A 中,实现该方法来做某事(例如打印调试语句):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.i("Test", "Did this work???");
    //TODO send notification to your server to verify this works?
}
于 2013-02-27T02:32:47.570 回答
5

我认为这种行为没有任何问题。

如果您希望保留 的状态ActivityA,请使用 和 的onSaveInstanceState方法onRestoreInstanceState。有关更多详细信息,请参阅http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle上的活动生命周期。

另请参阅https://stackoverflow.com/a/10492967/332210以获得更深入的了解。

于 2013-03-01T03:57:00.507 回答
3

您可以尝试一件事,提供您的布局,然后在??onCreate()中完成其余的工作?onStart()如果它有效?

像:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
   }

      @Override
       protected void onStart() {
       // TODO Auto-generated method stub
    super.onStart();
    Log.i(TAG, "On Start .....");

      }

查看活动生命周期 在此处输入图像描述

于 2013-03-01T05:07:43.893 回答
1

也许你应该使用

Intent startIntent = new Intent(view.getContext(), ActivityB.class); 
startActivity(startIntent); 
finish() ;

Intent startIntent = new Intent(view.getContext(), ActivityA.class); 
startActivity(startIntent); 
finish() ;

每次后退或前进。

于 2013-02-21T14:50:05.793 回答
1

它也面临着确切的问题并通过使用android:launchMode="standard"in activityof解决了问题manifest

于 2013-02-28T07:50:52.900 回答
1

覆盖 Activity A 中的 onStart() 和 onResume 方法并检查问题是否仍然存在。如果可能,请在此处提供您的活动 A 和 B 代码。

于 2013-03-01T06:10:28.097 回答
1

活动 A 使用布局 R.layout.mesajlar_list

活动 B 使用布局 R.layout.mesajdetaylari

但两者都有以下代码行:

kapatButton = (ImageView) findViewById(R.id.kapat_button);

R.id.kapat_button 在哪个布局?在不同的布局中使用相同的 id 是一件非常冒险的事情。我不能保证它会导致您所看到的,但它可能会导致奇怪的行为。

于 2013-03-01T19:06:50.977 回答
1

我认为这不是因为内存的限制。

https://www.box.com/s/7pd0as03bb8wwumuc9l9

您应该测试这两个活动并检查它是否也在此示例中发生。也请分享您的 AndroidManifest.xml 文件内容,这将有助于调试。

于 2013-03-02T11:01:46.413 回答
0

我最近遇到了这个问题,这让我很生气。我认为该问题围绕 2 个选项解决方案进行检查但无用。

关于此处更正的“不保留活动”设置,我使用此代码检查它是否可选检查(我的测试设备基于版本 2.3.5 自定义并且不显示此选项):

private boolean isAlwaysFinishActivitiesOptionEnabled() {
    int alwaysFinishActivitiesInt = 0;
    if (Build.VERSION.SDK_INT >= 17) {
        alwaysFinishActivitiesInt = Settings.System.getInt(getApplicationContext().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
    } else {
        alwaysFinishActivitiesInt = Settings.System.getInt(getApplicationContext().getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
    }

    if (alwaysFinishActivitiesInt == 1) {
        return true;
    } else {
        return false;
    }
}

在我的情况下,结果检查是错误的。我还在运行应用程序时检查内存,但没有任何反应。

于 2014-11-20T00:42:04.653 回答
0

您可以android:launchMode="singleTop"在清单中使用。

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
于 2017-10-18T06:49:17.093 回答