我使用导航抽屉,片段数量。 我想调用片段的onResume(),其中我需要从服务器加载更新的数据。
答案 0 :(得分:6)
onResume()
被调用时, Activity onResume()
会被呼叫,您不必在onResume()
中致电Fragment
。
阅读本文以了解Fragment Lifecycle
如果你想从你的Fragment
那里做,你应该做这样的事情
@Override
public void onResume(){
super.onResume();
//OnResume Fragment
}
此外,如果你必须做一些一般的事情,你可以在Activity
上做Fragment
与-(NSURLSession *)sessionConfig{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
return session;
}
-(NSMutableURLRequest *)sessionReqData:(NSDictionary *)reqData{
NSURL *url = [NSURL URLWithString:kVETAPP_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:reqData options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
return request;
}
+(ServerCommuManager*) sharedServerCommuManager{
static ServerCommuManager *_sharedServerManager = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedServerManager = [[ServerCommuManager alloc]init];
});
return _sharedServerManager;
}
-(void) methodName:(NSMutableDictionary *) dictRequest{
NSDictionary *dictionaryRequest = [[NSDictionary alloc] initWithObjectsAndKeys:kPOST_UPDATE_APPOINTMENT_SERVICE,kPOST_SERVICE_NAME,
dictRequest,kREQUEST,nil];
NSMutableURLRequest *request=[self sessionRequestWithData:dictionaryRequest];
NSURLSessionDataTask *postDataTask = [[self sessionConfiguration] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(!error){
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*)response;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
if (httpResp.statusCode==kResponseStatusSuccess)
{
if ([responseDictionary count]>0){
dispatch_async(dispatch_get_main_queue(), ^{
});
}
}
else if(httpResp.statusCode==kResponseStatusUnAuthorized){
}
else if(httpResp.statusCode==kResponseStatusInvalidAuthentication){
}
else{
NSLog(@“TimeOut”);
}
}
else{
NSLog(@“Server Eror %@”,error);
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate createAppointment:kResponseStatusInternalServerError];
});
}
}];
[postDataTask resume];
}
}