Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

android - Why Activity is hanging while coming back from another activity's few fragments?

I have two activities Activity A and B, Activity A has 3 fragments namely a1,a2 and a3. a3 is the default selected fragment which shows videos, a2 contains one big textView and a small edittext in it and a1 contains a listview.

Now On activity A's title bar I have a button, and since it is on title bar it is visible from all 3 fragments. OnClick of this button I open Activity B. using Following function.

public void inviteUser() {
    if ( getUserType() == ACTIVE ) {
        Intent intent = new Intent(this, B.class);
        intent.putExtra(Constants.MEETING_ID, (long) session.getSessionId());
        intent.putExtra(Constants.INVITE_FROM_SESSION, true);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        this.startActivityForResult(intent, RESULT_OK);
    } else {
        Toast.makeText(this, getString(R.string.strNotAPresenterMsg), Toast.LENGTH_LONG).show();
    }
}

Inside B I call its setResult(ResultCode) and finish(). Now I have two problems when I start B from Fragments a1 and a2,

1. B takes too much time before returning back to A (i.e. after pressing back button or after finishing the work of B), 2. It doesn't show any dialog after returning until either I navigate to some other fragment or I rotate the device, once I do that it starts behaving normally and surprisingly,

when I start the activity B from fragment a3, it returns immediately and behaves normally.

I have tried overriding all the life cycle methods of these fragments and put the Log statements inside each method. Pain is that method calling sequence is normal and same for both the cases.

I have tried changing the flags of the intent but to no gain.

I have added falg android:configChanges="keyboardHidden|orientation" for all the activities of my app.

Hope I explained the problem, in case any one need any specific part of the code. please comment.I will edit the question and add that. Help guys!!!

EDIT I was testing in GingerBread 2.3.6 it was not working but I just checked that on emulator running JellyBean and it is working fine. Looks like the issue is related to OS version. Any idea ?

EDIT 2 One important point of observation is that When in B I press back button, after few mili seconds the onResume() of Previous Activity and their fragments are called. But even after that B is on Screen for some time and that seems like phone hanged for few seconds....

EDIT 3 Just got to know that onStop() and OnDestroy() of B are not called when I start the B from Fragment a1 or a2. However They are called perfectly when I am on a3. What could be the reason for this ?

The doc says that "The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user."

Since onStop() is not guaranteed the activity behaves abnormally when it is not called.. I also came to know that in HoneyComb+ devices it is always called. I have tried calling onStop() inside onPause() but that is not working. What I do now ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It was really hard for me to explain my problem to SO community and even harder was to understand the problem source but once I tracked the error source it just took few mins to solve the problem.

The problem was due to an included layout (say x) in the Activity's layout view. The x's visibility was being decided on the current fragment. It was visible on a3 and View.GONE on other two fragments. I removed that layout from a1 and a2 and the problem was solved...

However I still can't understand what it has to relate with the B's onStop and onDestroy which are now being called perfectly... If anyone can find than please share..

Hope the information can be helpful for someone else,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...